|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #41132 No way to get original object from within __clone()
Submitted: 2007-04-18 14:23 UTC Modified: 2016-12-31 00:10 UTC
Avg. Score:3.3 ± 0.7
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: wb at pro-net dot co dot uk Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.2.1 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2007-04-18 14:23 UTC] wb at pro-net dot co dot uk
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;
            // we have no way of getting the original
            // object do we cant do the following...
        $this->children = $clonedChildren;


Expected result:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-10-22 07:17 UTC] ninzya at inbox dot lv
++ for this.

Remark: I think it's still better to pass $originalThis to __clone() as a regular 
object with no lifted access restrictions to private/protected members (i.e. just 
pass it as regular fcall argument).
 [2016-12-31 00:11 UTC]
-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
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Mar 02 12:01:29 2024 UTC