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
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
39 - 2 = ?
Subscribe to this entry?

 
 [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-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 20:01:29 2024 UTC