#27100 [Opn->Bgs]: __clone is referencing object held inside a class instead of copying
| From: | andi@php.net | Date: | Tue, 03 Feb 2004 18:48:54 +0000 |
| Subject: | #27100 [Opn->Bgs]: __clone is referencing object held inside a class instead of copying | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-54345@lists.php.net to get a copy of this message | ||
ID: 27100
Updated by: andi@php.net
Reported By: auroraeosrose at hotmail dot com
-Status: Open
+Status: Bogus
Bug Type: Zend Engine 2 problem
Operating System: *
PHP Version: 5CVS-2004-01-30 (dev)
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
Check the following bug report to see how we changed __clone()'s
behavior.
http://bugs.php.net/?id=26794
In any case, when we do a copy, we don't do a deep copy. You will have
to handle that yourself by adding such code to your __clone() method.
Previous Comments:
------------------------------------------------------------------------
[2004-01-30 17:05:56] auroraeosrose at hotmail dot com
Description:
------------
When a class holds an object of another class inside and the outside
class is cloned, the inside class seems to be referenced instead of
cloned along with the outside class
If you'll see below, you'll notice that the Test class is cloned
properly, the variable registered doesn't show up in $test but does in
$test2
However, the array added to the list inside the Test2 class shows up in
both, when it should only be in $test2
New php5 snap as of 1/30/2004 apache2 module - mysql only extra
extension loaded
Reproduce code:
---------------
class Test
{
private $object;
private $list = array();
public function __construct()
{
$this->object = new Test2();
}
public function registerVar($var)
{
$this->list[] = $var;
$key = end(array_keys($this->list));
$this->object->setList($key);
return;
}
}
class Test2
{
public $list = array('globals' => array());
public function setList($id)
{
$id = (int) $id;
$this->list[$id] = array();
return;
}
}
$test = new test();
$test2 = $test->__clone();
$test2->registerVar('hello');
print_r($test);
print_r($test2);
Expected result:
----------------
Test Object
(
[object:private] => Test2 Object
(
[list] => Array
(
[globals] => Array
(
)
)
)
[list:private] => Array
(
)
)
Test Object
(
[object:private] => Test2 Object
(
[list] => Array
(
[globals] => Array
(
)
[0] => Array
(
)
)
)
[list:private] => Array
(
[0] => hello
)
)
Actual result:
--------------
Test Object
(
[object:private] => Test2 Object
(
[list] => Array
(
[globals] => Array
(
)
[0] => Array
(
)
)
)
[list:private] => Array
(
)
)
Test Object
(
[object:private] => Test2 Object
(
[list] => Array
(
[globals] => Array
(
)
[0] => Array
(
)
)
)
[list:private] => Array
(
[0] => hello
)
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=27100&edit=1