note 36028 added to language.references

From: Date: Tue, 23 Sep 2003 12:51:05 +0000
Subject: note 36028 added to language.references
Groups: php.notes 
Request: Send a blank email to php-notes+get-56972@lists.php.net to get a copy of this message
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 !!! ?> ---- Manual Page -- http://www.php.net/manual/en/language.references.php Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+36028 Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+36028&report=yes Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+36028&report=yes

« previous php.notes (#56972) next »