ID:               48460
 Updated by:       j...@php.net
 Reported By:      ben at last dot fm
-Status:           Open
+Status:           Feedback
 Bug Type:         Scripting Engine problem
 Operating System: Linux
 PHP Version:      5.2.9
 New Comment:

Please try using this snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/




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

[2009-06-03 15:26:16] ben at last dot fm

Hmm.

The bug is that you can alter the behaviour of clone (that is, force
$d-
>list to be copied-by-reference instead of copied-by-value) merely by 
creating any other reference to $a->list. 'clone' is, therefore, the 
only language construct (that I know of) which is capable of this, and

is NOT the same as "$b = new A; $b->list = $a->list". 

There is in fact no way to tell whether a property is a reference or
not 
without cloning the entire object and seeing what happens when you 
change the clone's property; or by forcibly unsetting and resetting the

property.

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

[2009-06-03 13:54:41] col...@php.net

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.



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

[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