#48460 [Opn]: Referencing an object property causes behaviour change after clone

From: Date: Wed, 03 Jun 2009 13:34:44 +0000
Subject: #48460 [Opn]: Referencing an object property causes behaviour change after clone
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-138007@lists.php.net to get a copy of this message
ID: 48460 User updated by: ben at last dot fm Reported By: ben at last dot fm Status: Open -Bug Type: Arrays related +Bug Type: Variables related Operating System: Linux PHP Version: 5.2.9 New Comment: Sorry, put this in the wrong category. Previous Comments: ------------------------------------------------------------------------ [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

« previous php.bugs (#138007) next »