Re: [PHP-LANG] Object references
[email protected] (Andi Gutmans) Thu, 21 Mar 2002 22:38:17 +0200
| Newsgroups | php.lang |
|---|---|
| Message-ID | <[email protected]> |
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