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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
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-2024 The PHP Group
All rights reserved.
Last updated: Wed Apr 24 06:01:29 2024 UTC