php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #62775 Not documented and confusing: __clone()
Submitted: 2012-08-08 08:30 UTC Modified: 2012-08-10 13:35 UTC
From: olivier dot pons at gmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Linux
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: olivier dot pons at gmail dot com
New email:
PHP Version: OS:

 

 [2012-08-08 08:30 UTC] olivier dot pons at gmail dot com
Description:
------------
What is confusing (and is not documented yet) is that when you're in the "__clone" function the left operand concerns the destination (= new object) and the right operand concerns the source (= object to be cloned) _and_ you still use "$this"!

Sample:

    public function __clone()
    {
        $this->myObj = clone $this->myObj;
    }   


It's confusing: assigning $this->variable to $this->variable.

Test script:
---------------
public function __clone()
{
    $this->myObj = clone $this->myObj;
}   


Expected result:
----------------
A note in http://php.net/clone section.

Actual result:
--------------
Not documented yet.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-10 10:26 UTC] maus at hab dot de
Erm.. Why do you think the $this at the right hand side refers to the source?

$this->myObj = clone $this->myObj

Assign *a clone* of $this's propery `myObj' to $this's property `myObj'.

Manual:

"When an object is cloned, PHP 5 will perform a shallow copy of all of the object's properties. Any properties that are references to other variables, will remain references."

http://php.net/manual/en/language.oop5.cloning.php

If `myObj' contains an object then the clone() of the containing object creates an new object whose property `myObj' contains the same object as the original.

<?php

class Foo
{
    public $bar;
}

class Bar
{
}

$a = new Foo();
$a->bar = new Bar();

$b = clone($a);
echo ($a->bar === $b->bar) ? "Same objects\n" : "Different objects\n";

=> "Same objects"

<?php

class Foo
{
    public $bar;

    public function __clone ()
    {
            $this->bar = clone($this->bar);
    }
}

class Bar
{
}

$a = new Foo();
$a->bar = new Bar();

$b = clone($a);
echo ($a->bar === $b->bar) ? "Same objects\n" : "Different objects\n";

=> "Different objects"

You are, in fact, not "assigning $this->variable to $this->variable" but assigning the return value of calling clone($this->variable) to $this->variable.
 [2012-08-10 13:35 UTC] olivier dot pons at gmail dot com
Oh ok I see your point. The documentation was confusing to me. You can remove my bug report.
 [2012-08-10 13:35 UTC] olivier dot pons at gmail dot com
-Status: Open +Status: Closed
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Thu Oct 08 00:00:02 2026 UTC