ID:               48460
 Updated by:       col...@php.net
 Reported By:      ben at last dot fm
 Status:           Open
 Bug Type:         Variables related
 Operating System: Linux
 PHP Version:      5.2.9
 New Comment:

This is quite expected, that's basically the same as:

$a = array(2);
$d = &$a[0];

$a_copy = $a;
$a_copy[0]++;

echo $a[0],$d; // 3 3

You can do a deep copy using the __clone hook.

OTOH I'm not sure if clone shouldn't separate the properties directly,
instead of simply assigning them.




Previous Comments:
------------------------------------------------------------------------

[2009-06-03 13:34:44] ben at last dot fm

Sorry, put this in the wrong category.

------------------------------------------------------------------------

[2009-06-03 13:32:35] ben at last dot fm

More minimal test case:

<?php
class A { var $p; }

$a = new A;
$a->list = 1;

$b = clone $a;
$link =& $a->p;
unset($link);
$c = clone $a;
$link =& $a->p;
$d = clone $a;
unset($link);
$e = clone $a;

$b->p = 2;
$c->p = 3;
$d->p = 4;
$e->p = 5;

echo "$a->p\n$b->p\n$c->p\n$d->p\n$e->p\n";

------------------------------------------------------------------------

[2009-06-03 13:29:02] ben at last dot fm

Description:
------------
If you create a reference to a property of an object, then clone 
the object, the reference will become bi-directional and the property 
will no longer behave like a property.

This does not happen if you create, then destroy, the reference BEFORE

cloning. 

This persists even if you unset the reference AFTER cloning.

Reproduce code:
---------------
<?php
class A { var $list; }

$a = new A;
$a->list = array( 1, 1, 1, 1, 1 );

$b = clone $a;
$link =& $a->list;
unset($link);
$c = clone $a;
$link =& $a->list;
$d = clone $a;
unset($link);
$e = clone $a;

$b->list = array(2,2,2,2,2);
$c->list = array(3,3,3,3,3);
$d->list = array(4,4,4,4,4);
$e->list = array(5,5,5,5,5);

echo "$a\n$b\n$c\n$d\n$e\n";

Expected result:
----------------
{1}
{2}
{3}
{4}
{5}

Actual result:
--------------
{5}
{2}
{3}
{5}
{5}


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=48460&edit=1

Reply via email to