#27100 [Opn->Csd]: shallow __clone copy behavior not documented in any place on php website
| From: | curt@php.net | Date: | Sat, 07 Aug 2004 17:45:22 +0000 |
| Subject: | #27100 [Opn->Csd]: shallow __clone copy behavior not documented in any place on php website | ||
| References: | 1 | Groups: | php.doc |
| Request: | Send a blank email to phpdoc+get-969362878@lists.php.net to get a copy of this message | ||
ID: 27100
Updated by: curt@php.net
Reported By: auroraeosrose at hotmail dot com
-Status: Open
+Status: Closed
Bug Type: Documentation problem
Operating System: *
PHP Version: 5CVS-2004-01-30 (dev)
New Comment:
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.
Thank you for the report, and for helping us make our documentation
better.
zend-engine2.php is old documentation. The changes this will be updated
at http://php.net/oop5.cloning when the manual is rebuilt.
Previous Comments:
------------------------------------------------------------------------
[2004-02-04 18:02:45] auroraeosrose at hotmail dot com
If this is the default behavior, it should be documented in some way.
I'm aware that most of php5 is undocumented at this point, but the zend
php5 changes page at http://www.php.net/zend-engine-2.php makes no
mention that __clone is a shallow and not a deep copy - there should be
a short paragraph that objects stired as properties are NOT cloned but
instead referenced unless you override within a __clone method. It
will save a lot of bug reports :) Random bug reports are not where
people go looking for documentation on how __clone works, and according
to the current docs, this is a bug, not a feature.
------------------------------------------------------------------------
[2004-02-03 13:48:54] andi@php.net
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.
------------------------------------------------------------------------
[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