On Sat, 2004-08-21 at 11:21, Curt Zirzow wrote:
> * Thus wrote Robert Cummings:
> > 
> > As exemplified in the sample script I sent in my last response and by
> > the link sent by Hannes Magnusson, in PHP5 the following have identical
> > behaviour:
> > 
> >     $o1 = new Foo();
> >     $o2 = $o1;
> > 
> >     // Is same as...
> > 
> >     $o1 = new Foo();
> >     $o2 =& $o1;
> 
> But = and =& are not identical:
> 
> <?php
> class foo { };
> 
> $a = new foo();
> $b = $a;
> 
> var_dump($b); /* object #1 */
> $a = null;
> var_dump($b); /* object #1 */
> 
> $c = new foo();
> $d = &$c;
> 
> var_dump($d); /* object #2 */
> $c = null;
> var_dump($d); /* NULL */

Hmmm this is a different situation than you first presented. Here you
are modifying the variable and not unsetting it. Unsetting it does the
expected symbolic unlinking. However it seems (probably for
compatibility reasons) that PHP will not overwrite an object which was
symbolically linked without a reference. Now I can see what to watch out
for and indeed this isn't obvious :) To illustrate the confusion I've
modified your example to show that indeed we have a reference when
modifying the object (but as said re-assignment as "special" behaviour):

<?php

class foo { var $member = 1; };

$a = new foo();
$b = $a;

$a->member = 10;
var_dump($b); /* object #1 */
$a = null;
var_dump($b); /* object #1 */

$c = new foo();
$d = &$c;

$c->member = 20;
var_dump($d); /* object #2 */
$c = null;
var_dump($d); /* NULL */

We know that $b is a reference to $a because when we modified the member
var using $a->member then $b's member was also changed -- thus $b is the
same object as $a.

Cheers,
Rob.
-- 
.------------------------------------------------------------.
| InterJinn Application Framework - http://www.interjinn.com |
:------------------------------------------------------------:
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for       |
| creating re-usable components quickly and easily.          |
`------------------------------------------------------------'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to