php.net |  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
Votes:7
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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: wb at pro-net dot co dot uk
New email:
PHP Version: OS:

 

 [2007-04-18 14:23 UTC] wb at pro-net dot co dot uk
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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

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] 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
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC