#33128 [Opn->Bgs]: Cloning of objects containing other objects leads to referencing

From: Date: Tue, 24 May 2005 16:31:14 +0000
Subject: #33128 [Opn->Bgs]: Cloning of objects containing other objects leads to referencing
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-79448@lists.php.net to get a copy of this message
ID: 33128 Updated by: derick@php.net Reported By: p dot seizov at decart dot bg -Status: Open +Status: Bogus Bug Type: Class/Object related Operating System: Windows, Linux PHP Version: 5.0.4 New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php . Previous Comments: ------------------------------------------------------------------------ [2005-05-24 17:11:56] p dot seizov at decart dot bg Description: ------------ If you clone an object containing another object, the second object is actually referenced to the original. So if you change the cloned subobject, it will change the original. Reproduce code: --------------- class ParentObject { public $value; public $subObject; } class SubObject { public $value; } $subObject = new SubObject(); $subObject->value = 1; $parentObject = new ParentObject(); $parentObject->value = 1; $parentObject->subObject = clone $subObject; $clonedObject = clone $parentObject; $clonedObject->value = 2; $clonedObject->subObject->value = 2; echo $parentObject->value."\r\n"; echo $parentObject->subObject->value."\r\n"; Expected result: ---------------- 1 1 Actual result: -------------- 1 2 ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=33128&edit=1

« previous php.bugs (#79448) next »