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