note 94571 added to language.oop5.cloning
| From: | rarioj at gmail dot com | Date: | Thu, 12 Nov 2009 16:12:06 +0000 |
| Subject: | note 94571 added to language.oop5.cloning | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162809@lists.php.net to get a copy of this message | ||
The method __clone() described by "cheetah at tanabi dot org" works flawlessly. But one
thing to note that sometimes an object implements its own cloning logic. By
unserialize-serialize-ing this object will not invoke the internal __clone() method. This is the
revised version:
<?php
function __clone() {
foreach ($this as $key => $val) {
if (is_object($val)) {
if (method_exists($val, '__clone')) {
$this->{$key} = clone $val;
} else {
$this->{$key} = unserialize(serialize($val));
}
} else if (is_array($val)) {
$this->{$key} = unserialize(serialize($val));
}
}
}
?>
----
Server IP: 69.147.83.197
Probable Submitter: 125.167.108.115
----
Manual Page -- http://www.php.net/manual/en/language.oop5.cloning.php
Edit -- https://master.php.net/note/edit/94571
Del: integrated -- https://master.php.net/note/delete/94571/integrated
Del: useless -- https://master.php.net/note/delete/94571/useless
Del: bad code -- https://master.php.net/note/delete/94571/bad+code
Del: spam -- https://master.php.net/note/delete/94571/spam
Del: non-english -- https://master.php.net/note/delete/94571/non-english
Del: in docs -- https://master.php.net/note/delete/94571/in+docs
Del: other reasons-- https://master.php.net/note/delete/94571
Reject -- https://master.php.net/note/reject/94571
Search -- https://master.php.net/manage/user-notes.php