note 88186 added to language.oop5.cloning
| From: | Anonymous at osu1 dot php dot net | Date: | Wed, 14 Jan 2009 13:24:29 +0000 |
| Subject: | note 88186 added to language.oop5.cloning | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-149186@lists.php.net to get a copy of this message | ||
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...
----
Server IP: 222.73.227.205
Probable Submitter: 219.142.118.227
----
Manual Page -- http://www.php.net/manual/en/language.oop5.cloning.php
Edit -- https://master.php.net/note/edit/88186
Del: integrated -- https://master.php.net/note/delete/88186/integrated
Del: useless -- https://master.php.net/note/delete/88186/useless
Del: bad code -- https://master.php.net/note/delete/88186/bad+code
Del: spam -- https://master.php.net/note/delete/88186/spam
Del: non-english -- https://master.php.net/note/delete/88186/non-english
Del: in docs -- https://master.php.net/note/delete/88186/in+docs
Del: other reasons-- https://master.php.net/note/delete/88186
Reject -- https://master.php.net/note/reject/88186
Search -- https://master.php.net/manage/user-notes.php