#36885 [Fbk->Opn]: __clone() bug when it's define in class extending arrayObject SPL class

From: Date: Tue, 28 Mar 2006 08:34:36 +0000
Subject: #36885 [Fbk->Opn]: __clone() bug when it's define in class extending arrayObject SPL class
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-94959@lists.php.net to get a copy of this message
ID: 36885 User updated by: lta at lfdj dot com Reported By: lta at lfdj dot com -Status: Feedback +Status: Open Bug Type: SPL related Operating System: solaris PHP Version: 5.1.2 New Comment: Forget to put run code... This is to insert after test class definition. $original = new test; $original->append(new bidon()); $original->append(new bidon()); $cloned2 = clone $original; Previous Comments: ------------------------------------------------------------------------ [2006-03-28 10:28:10] tony2001@php.net I don't see any problems: <?php class test extends ArrayObject { public $pub = 1; public function __clone() { $this->pub = "cloned"; } } $a = new test; $b = clone $a; var_dump($a->pub); var_dump($b->pub); ?> int(1) string(6) "cloned" Did you even try to run your code? I doesn't work at all as you didn't instanciate the objects. ------------------------------------------------------------------------ [2006-03-28 09:51:14] lta at lfdj dot com Description: ------------ When a __clone() function is defined for a class that extends arrayObject from SPL, there is no way to clone the objects contained in arrayObject. If you do same code for class not extending the SPL arrayObject but any other objectContainer, there is no problem. Reproduce code: --------------- class bidon { public $foo = "bidon 1"; } class test extends ArrayObject { public $pub = 1; public function __clone() { $this->pub = "cloned"; foreach ($this as $k=>$item) { $this[$k]= clone $item; $this[$k]->foo++; } } } if($original === $cloned2) { echo "bad master clone# $original $cloned2<br>"; } else { echo "good master clone<br>";} foreach ($original as $k=>$obj) { if($original[$k] === $cloned2[$k]) { echo "bad clone childs# $original[$k] $cloned2[$k]<br>"; } else { echo "good child clone<br>";} } Expected result: ---------------- good master clone good child clone good child clone Actual result: -------------- good master clone bad clone childs# Object id #7 Object id #7 bad clone childs# Object id #2 Object id #2 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=36885&edit=1

« previous php.bugs (#94959) next »