Re: Object references

From: Date: Thu, 21 Mar 2002 20:38:17 +0000
Subject: Re: Object references
References: 1  Groups: php.lang 
Request: Send a blank email to php-lang+get-273@lists.php.net to get a copy of this message
At 10:47 21/03/2002 +0100, Lucijan wrote:
I can't understand why this works like it does. class MyObj { var $Name; function MyObj($Name) {
    $this->Name = $Name;
} } $a = new MyObj('MyObjName'); $b = $a; $b->Name = 'NoName'; echo $a->Name; Last line will output 'MyObjName' instead of 'NoName' (like I was expecting) I'd expect $a and $b to behave like reference pointers (change in $b is reflected in $a and vice versa) instead of copiing $a to $b (beiing a Delphi programmer this is as natural to me as the Sun raising in east and setting in west :-) So, is there a way to do this; pointers or something maybe?
You can do $b =& $a; to make it a reference to the same thing. In the Zend Engine 2 objects will be handles so your example will work the way you expect and you'll need to clone objects explicitly by doing $a->__clone() Andi

« previous php.lang (#273) next »