note 88186 deleted from language.oop5.cloning by danbrown
| From: | danbrown@php.net | Date: | Wed, 14 Jan 2009 17:46:15 +0000 |
| Subject: | note 88186 deleted from language.oop5.cloning by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-149187@lists.php.net to get a copy of this message | ||
Note Submitter: Anonymous
----
To cheetah:
do u really think so?Please look at this:
<?
class A
{
public $object;
function __construct ()
{
$this->object = new B($this);
}
public function __clone()
{
foreach($this as $key => $val) {
if(is_object($val)||(is_array($val))){
$this->{$key} = unserialize(serialize($val));
}
}
}
}
class B
{
public $object;
function __construct ($parent = NULL)
{
$this->object = $parent;
}
}
$A = new A();
$B = $A->object;
echo $A;
echo $B;
echo $B->object;
$newA = clone $A;
$newB = $newA->object;
echo $newA;
echo $newB;
echo $newB->object;
?>
This will output like this:
Object id #1
Object id #2
Object id #1
Object id #3
Object id #4
Object id #5
Don't you notice the problem?
At first A has a reference to B, and B has a reference to A.
But after we clone object A..
What's happened?
B references to a object other than A..
So, what a pity we failed to do deep clone by your way...