note 89321 deleted from language.oop5.cloning by cmb
| From: | cmb@php.net | Date: | Sun, 10 Jul 2016 20:19:33 +0000 |
| Subject: | note 89321 deleted from language.oop5.cloning by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-205958@lists.php.net to get a copy of this message | ||
Note Submitter: koyama
----
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!'.