note 36030 deleted from language.references.arent by didou

From: Date: Tue, 23 Sep 2003 14:24:24 +0000
Subject: note 36030 deleted from language.references.arent by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-56978@lists.php.net to get a copy of this message
Note Submitter: sieglt@unreal64.net ---- Here is an example how to get reference to an object which is referenced by other object using calling method. You can see that GetItem2 method has argument passed by reference. You will obtain copy of the original object not reference to the original object. The only right way what i have found is using fuction returning reference. See method &GetItem returning reference to the object. <? class A { var $item; function A(&$b) { $this->item=&$b; } function &GetItem() { return($this->item); } function GetItem2(&$Dst) { $Dst=$this->item; } }; class B { var $value; function SetVal($val) { $this->value=$val; } function PrintMe() { print $this->value; print "\n"; } }; $original=&new B(); $original->SetVal(20); $original->PrintMe(); // prints 20 $ref=&new A($original); $newref=&$ref->GetItem(); $newref->SetVal(30); $original->PrintMe(); // prints 30 unset($newref2); $ref->GetItem2($newref2); $newref2->SetVal(40); $original->PrintMe(); // prints 30 NOT 40 as we could expect !!! ?>

« previous php.notes (#56978) next »