Re: OO in PHP5.0 - Referencing Object or not?!
| From: | Daniel Schierbeck | Date: | Sun, 22 Aug 2004 20:07:37 +0000 |
| Subject: | Re: OO in PHP5.0 - Referencing Object or not?! | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-194430@lists.php.net to get a copy of this message | ||
L0t3k wrote:
Raffael,<?php class Foo { public $foo = "bar"; } $obj1 = new Foo; $obj2 = $obj1; $obj3 = $obj2; $obj2 = NULL; echo $obj1->foo; ?> outputs bar while <?php class Foo { public $foo = "bar"; } $obj1 = new Foo; $obj2 =& $obj1; $obj3 =& $obj2; $obj2 = NULL; echo $obj1->foo; ?> outputs Notice: Trying to get property of non-object in /free1go/a/o/www.aoide.1go.dk/lab/bar.php5 on line 16 Hence, there is a difference between references and object handles. When you pass by reference,for instance $foo =& $bar, where $bar is an object, $foo will point to whatever $bar is pointing to. When you write $foo = $bar, $foo will point to the same object as $bar, but $foo is not forever bound to $bar, as in the first example. It simply has the same object handle. Therefore, when you delete $bar ($bar = NULL), $foo will be intact when using =, but will be set to NULL as well when using =&. Cheers, Danielobject in PHP5 _are_ passed by reference. internally, objects arehandles unique to a request, so all youre doing is passing a handle around. note, however, that simple variable access will _always_ be faster than method calls. this is true in C as well as PHP, except in PHP the effects are more noticeable since it is interpreted rather than compiled. l0t3k Just to get things straight: