note 36028 rejected from language.references by ali

From: Date: Tue, 23 Sep 2003 12:57:47 +0000
Subject: note 36028 rejected from language.references by ali
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-56974@lists.php.net to get a copy of this message
Note Submitter: sieglt@unreal64.net ---- Strange references behaviour when using objects. Here is a simple example how to get reference to an object which is referenced by other object by 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 discovered is using fuction returning reference. See &GetItem. Will php5 solve this problem? <? 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 !!! ?>

« previous php.notes (#56974) next »