|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2011-10-22 07:17 UTC] ninzya at inbox dot lv
[2016-12-31 00:11 UTC] cmb@php.net
-Summary: No way to get origional object from within __clone()
+Summary: No way to get original object from within __clone()
-Package: Feature/Change Request
+Package: Scripting Engine problem
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 20:00:01 2025 UTC |
Description: ------------ When you clone an object the __clone method does not have a way to get a reference to the original object. It would be advantageous to have a way to get the original object from within the __clone method. One reason for this is that you can have an object that has child objects that in-turn reference back to the parent object and on clone you want to clone these child objects but would need to remove the original reference and replace it with the new parent reference. This feature could be implemented by simply passing the original object as an argument to the __clone() method. Reproduce code: --------------- class Parent { protected $children = array(); public function __clone($originalThis) { $clonedChildren = array(); foreach($this->children as $child) { $childClone = clone $child; $childClone->attach($this); // we have no way of getting the original // object do we cant do the following... $childClone->detach($originalThis); } $this->children = $clonedChildren; } } Expected result: ---------------- N/A Actual result: -------------- N/A