note 89321 added to language.oop5.cloning
| From: | koyama at osu1 dot php dot net | Date: | Tue, 03 Mar 2009 15:28:18 +0000 |
| Subject: | note 89321 added to language.oop5.cloning | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151398@lists.php.net to get a copy of this message | ||
The __clone() method for deep cloning by cheetah at tanabi dot org also works when the object to be
cloned contains references to itself. This is not the case for any variation of the __clone() method
in edit by danbrown at php dot net.
We are taking advantage of the fact that one can serialize an object that references itself.
Example:
<?php
class Foo
{
function __construct()
{
$this->_myself = $this;
}
function __clone() {
foreach ($this as $key => $val) {
if (is_object($val) || (is_array($val))) {
$this->{$key} = unserialize(serialize($val));
}
}
}
}
// this object references itself
$foo = new Foo();
// create a deep clone
$bar = clone $foo;
// check if we reach this point
echo 'Finished cloning!';
?>
Replacing the __clone() method with the one shown in edit by danbrown at php dot net we run into an
infinite loop, and we never get message 'Finished cloning!'.
----
Server IP: 87.54.44.10
Probable Submitter: 217.157.204.183
----
Manual Page -- http://www.php.net/manual/en/language.oop5.cloning.php
Edit -- https://master.php.net/note/edit/89321
Del: integrated -- https://master.php.net/note/delete/89321/integrated
Del: useless -- https://master.php.net/note/delete/89321/useless
Del: bad code -- https://master.php.net/note/delete/89321/bad+code
Del: spam -- https://master.php.net/note/delete/89321/spam
Del: non-english -- https://master.php.net/note/delete/89321/non-english
Del: in docs -- https://master.php.net/note/delete/89321/in+docs
Del: other reasons-- https://master.php.net/note/delete/89321
Reject -- https://master.php.net/note/reject/89321
Search -- https://master.php.net/manage/user-notes.php