PHP - Cloing Class Property
hey guys i havent done any cloneing before but what im trying to do is clone $this->_view property in my Action class and pass it to my News_Controller class so i can call the View class by a varaible and not a property....can anyone help me and give me some advise on how i should do this...i tried below but i cant get it to work...thanks alot
news controller class Code: [Select] <?php class News_Controller extends Action { public function articles() { $view->title = "hello"; } } ?> action class Code: [Select] <?php class Action { protected $_request; protected $_response; public $_view; public function __construct(Request $request, Response $response) { $this->set_request($request)->set_view(); $view = $this->clone_view(); $view->title = "hello"; $this->_response = $response; } protected function set_request(Request $request) { $this->_request = $request; return $this; } protected function set_view() { $this->_view = new View; return $this; } protected function get_request() { return $this->_request; } public function clone_view() { $view = clone $this->_view; return $view; } public function dispatch($method, $parameters) { if ($this->get_request()->is_dispatched()) { if(method_exists($this, $method)) { call_user_func_array(array($this, $method), $parameters); } else { } } } } ?> Similar TutorialsHi there people of the PHP forum, This functions intention is to output the same code 4 digit code twice before generating a new one, I can get it to do one fine, but I cannot for the life of me figure out how to get it to repeat twice before changing on the third call. private function KeyGen(){ $Numerical = range(0,9); $Alpha = range("A","Z"); $Keys = array_merge($Numerical, $Alpha); return $Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)].$Keys[rand(0,35)]; } I am needing it to do this so that each 'set' of 2 calls to the function (within the class) gives the same 4 digit code. At the moment I am just making one call & then assigning that single call to a variable and then referencing that var twice, this is not what I want to do, but it scratches the itch for now... Any idea's/other logic would be greatly appreciated. Cheers, Rw I have mysqli object in Database class base: [color=]database class:[/color] class Database { private $dbLink = null; public function __construct() { if (is_null($this->dbLink)) { // load db information to connect $init_array = parse_ini_file("../init.ini.inc", true); $this->dbLink = new mysqli($init_array['database']['host'], $init_array['database']['usr'], $init_array['database']['pwd'], $init_array['database']['db']); if (mysqli_connect_errno()) { $this->dbLink = null; } } } public function __destruct() { $this->dbLink->close(); } } Class derived is Articles where I use object dBLink in base (or parent) class and I can't access to mysqli methods (dbLink member of base class): Articles class: require_once ('./includes/db.inc'); class Articles extends Database{ private $id, .... .... $visible = null; public function __construct() { // Set date as 2009-07-08 07:35:00 $this->lastUpdDate = date('Y-m-d H:i:s'); $this->creationDate = date('Y-m-d H:i:s'); } // Setter .... .... // Getter .... .... public function getArticlesByPosition($numArticles) { if ($result = $this->dbLink->query('SELECT * FROM articles ORDER BY position LIMIT '.$numArticles)) { $i = 0; while ($ret = $result->fetch_array(MYSQLI_ASSOC)) { $arts[$i] = $ret; } $result->close(); return $arts; } } } In my front page php I use article class: include_once('./includes/articles.inc'); $articlesObj = new articles(); $articles = $articlesObj->getArticlesByPosition(1); var_dump($articles); [color=]Error that go out is follow[/color] Notice: Undefined property: Articles::$dbLink in articles.inc on line 89 Fatal error: Call to a member function query() on a non-object in articles.inc on line 89 If I remove constructor on derived class Articles result don't change Please help me Ok. I know you can pass the object of a class as an argument. Example: class A { function test() { echo "This is TEST from class A"; } } class B { function __construct( $obj ) { $this->a = $obj; } function test() { $this->a->test(); } } Then you could do: $a = new A(); $b = new B($a); Ok so that's one way i know of. I also thought that you could make a method static, and do this: (assuming class A's test is 'static') class B { function test() { A::test(); } } But that is not working. I'd like to know all possible ways of accomplishing this. Any hints are appreciated. thanks Hi Can you call Class A's methods or properties from Class B's methods? Thanks. I have an existing instance of my class Database, now I want to call that instance in my Session class, how would I go about doing this? If a class has a constructor but also has a static method, if I call the static method does the constructor run so that I can use an output from the constructor in my static method? --Kenoli Hi, I need to be able to call a class based on variables. E.G. I would normally do: Code: [Select] $action = new pattern1() but i would like to be able to do it dynamicaly: Code: [Select] $patNum = 1; $action = new pattern.$patNum.() Im wondering if that's possible? If so what would the correct syntax be? Many Thanks. I have two classes: ## Admin.php <?php class Admin { public function __construct() { include("Config.php"); } /** * deletes a client * @returns true or false */ function deleteClient($id) { return mysql_query("DELETE FROM usernames WHERE id = '$id'"); } } ?> ## Projects.php <?php class Projects { public function __construct() { include("Config.php"); $this->admin = $admin; $this->dataFolder = $dataFolder; } /** * Deletes a project * @returns true or false */ function deleteProject($id) { $root = $_SERVER['DOCUMENT_ROOT']; $theDir = $root . $this->dataFolder; $sql = mysql_query("SELECT * FROM projectData WHERE proj_id = '$id'"); while ($row = mysql_fetch_array($sql)) { $mainFile = $row['path']; $thumb = $row['thumbnail']; if ($thumb != 'null') { unlink($theDir . "/" . substr($thumb,13)); } unlink($theDir . "/" . substr($mainFile,13)); } $delete = mysql_query("DELETE FROM projectData WHERE proj_id = '$id'"); $getDir = mysql_query("SELECT proj_path FROM projects WHERE id = '$id'"); $res = mysql_fetch_array($getDir); rmdir($theDir . "/" . $res['proj_path']); return mysql_query("DELETE FROM projects WHERE id = '$id'"); } } ?> How can I call deleteProject() from within Admin.php? Hi people! class FirstOne{ public function FunctionOne($FirstInput){ //do stuff and output value return $value1; } } Then:- class SecondOne{ public function FunctionTwo($AnotherInput){ //do stuff and output value return $value2; } } What I want to know is this, if I want to use FunctionOne() in Class SecondOne do I do it like this:- (Assume as I have instantiated the first class using $Test = new FirstOne(); ) class SecondOne{ function SecondedFunction(){ global $Test; return $Test->FunctionOne(); } public function FunctionTwo($AnotherInput){ //do stuff and output value return $value2; } public function FunctionThree(){ //some code here $this->Test->SecondedFunction();<--I think as I can omit the $this-> reference } } My point is: Do I have to do it this way or is there way of having this done through __construct() that would negate the need for a third party function? I have a version working, I just think that it is a little convoluted in the way as I have done it, so I thought I would ask you guys. Any help/advice is appreciated. Cheers Rw First let me just say, I am by no means a coder. But I am trying to set up a website for a friend.
I am no novice when it comes to the web design part, but I am one when it comes to PHP code.
That said, I have a joomla site on my local machine for the time being. I am using joomla for this site. Joomla wont help me with this problem. They refer me to the creator of the extension.
Well, problem is, I have posted there and have not had any answer yet either. The problem is when I use a form component and ue the select box option.
Anyway, the problem I am having is:
Notice: Undefined property: stdClass::$listitemid in C:\xampp\htdocs\joomla30\administrator\components\com_visforms\helpers\html\visforms.php on line 505 Notice: Undefined property: stdClass::$listitemvalue in C:\xampp\htdocs\joomla30\administrator\components\com_visforms\helpers\html\visforms.php on line 505 Notice: Undefined property: stdClass::$listitemlabel in C:\xampp\htdocs\joomla30\administrator\components\com_visforms\helpers\html\visforms.php on line 505Here is the code I found the $returnopts[] = array( 'id' => $option->listitemid, 'value' => $option->listitemvalue, 'label' => $option->listitemlabel, 'selected' => $selected);What is causing this problem? How can I fix this? Edited by CrazyYoda, 30 December 2014 - 05:50 PM. Hey Guys.
I have a class named CoreCartFunctions. In the class there is a protected property named $menu_item_id which is initialized as null.
A method named GetMenuItemId assigns the $menu_item_id its value. I have tested it out using the following, to see if a value got returned and it did
fb($menu_item_id->GetMenuItemId(), "This is the menu id");The problem is when I access the property from a different public method in the same class it dones't return anything. fb($menu_item_id->DisplayMenuItems());It only returns something when I hard code a value to it.... Not sure why this is happening. Here is my full code. Please not the example below may have some syntax errors since I just copied and paste pieces of my code, to show a quick and dirty example. class CoreCartFunctions { protected $menu_item_id = NULL; public static $items; //Equal to the a long session string protected function GetMenuItemId() { foreach (self::$items as $menu_item_id_session) { /*********************************** Get the id of the menu item item ************************************/ $this->menu_item_id = preg_match_all('/\-(.*?)\*/',$menu_item_id_session,$match_found)?$match_found[1][0]:""; // The following line shows example of above line // $menu_item_id = "12"; public function DisplayMenuItems(){ return $this->menu_item_id; // Doesn't return Item ID } } I've been getting this error message: Trying to get property of non-object in /application/controllers/index.php on line 37 Here is line 37 $this->view->title = $pageInfo[0]->title; any help would be much appreciated. Hi all, I have a script which I coded not long ago which works fine but there is one error which says: Notice: Trying to get property of non-object in /home/www/***-*****.com/hts.php on line 374 I carnt see why its showing the error. $fetch2=mysql_query("SELECT crew FROM users WHERE username='$username'") or die (mysql_error()); $fetch1 = mysql_fetch_object($fetch2); $crewrep1=mysql_query("SELECT * FROM crews WHERE name='$fetch1->crew'") or die (mysql_error()); $crewrep = mysql_fetch_object($crewrep1); $result = $rep + $reps; $result55 = $crewrep->rep + $reps; // Line 374 Can anyone else see why its showing the error? Thanks for your help. Hello, I'm currently in the process of trying to learn php. I've been working on an issue with something I am trying to code from a book I purchased "PHP and MySQL Web Development 5th Ed." by Luke Welling and Laura Thomson. So far the book has been great, but I'm trying to figure out using MySQL and php. When I try to run this particular script for a query for a book store, I get the error: Notice: Trying to get property of non-object in C:\php-book\ch11\results.php on line 37. This is the code: Code: [Select] $query = "select * from books where ".$searchtype."like '%".$searchterm."%'"; $result = $db->query($query); $num_results = $result->num_rows; echo "<p>Number of books found: ".$num_results."</p>"; for($i=0; $i<$num_results; $i++) { $row = $result->fetch_assoc(); echo "<p><strong>".($i+1).". Title: "; echo htmlspecialchars(stripslashes($row['title'])); echo "</strong><br />Author: "; echo stripslashes($row['author']); echo "<br /> ISBN: "; echo stripslashes($row['isbn']); echo "<br />Price: "; echo stripslashes($row['price']); echo "</p>"; } $result->free(); $db->close(); Line 37 is specifically: $num_results = $result->num_rows; I've been bashing my head for a couple of hours here, thank you in advance to whoever can offer any help. Hi, I was wondering if someone can help me, I keep getting these errors. Notice: Undefined property: stdClass::$vOptionType in /default/commonFunctions.php on line 267 That is the line from the file. if($rsOptions[$j]->vOptionType=='colour_fromcsv' || $rsOptions[$j]->vOptionType=='size_fromcsv') I was wondering if someone could tell me what it means and possible point me in the right direction on how to fix the issue. Thanks, Jordan For example: class People{ var $them = array(); function groupPeople(){ foreach($this->them as $him){ echo $him.", "; } } } $people = new People; $people->them = array('Harry','Todd'); $people->them = array('Tom','Jerry'); $people->groupPeople(); // result: Tom, Jerry, I think this code is working. However, I need to merge those array above, array('Harry','Todd') and array('Tom','Jerry') So I can get array('Harry','Todd','Tom','Jerry') and a list of names as a result. Maybe I should use array_merge, but how? A little help would be appreciated. Thank you in advanced, ayok We have a complex, json object which is multiple levels deep. We need to search for a given property/key in the entire object (not just first occurrence, but all), and if we find the property, return where in the object each property exists, so we can modify the properties value. Figure it needs to be recursive. Hey Guys. I am trying to assign a property through a method that is called in a swtich method.
After the property gets "assigned" I do a var_dump on the property and it returns NULL?
I am not sure why it returns NULL.... I would really appreciate any help...
class Tea { public $current_tea; //Current tea does not get assigned when using the AssignTea($TeaArg) function private $upcoming_tea = "Blacktea"; public function SelectTea() { $TeaType = "Black"; switch ($TeaType) { case "Black": $this->AssignTea($this->upcoming_tea); break; }// End of Swtich }// End of function SelectTea() private function AssignTea($TeaArg){ $this->current_tea = $TeaArg; } } $tea = new Tea(); var_dump($tea->current_tea); Edited by eldan88, 20 May 2014 - 10:07 AM. |