Doc #62775 [Opn->Csd]: Not documented and confusing: __clone()
| From: | olivier dot pons at gmail dot com | Date: | Fri, 10 Aug 2012 13:35:04 +0000 |
| Subject: | Doc #62775 [Opn->Csd]: Not documented and confusing: __clone() | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-8724@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=62775&edit=1
ID: 62775
User updated by: olivier dot pons at gmail dot com
Reported by: olivier dot pons at gmail dot com
Summary: Not documented and confusing: __clone()
-Status: Open
+Status: Closed
Type: Documentation Problem
Package: Documentation problem
Operating System: Linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Oh ok I see your point. The documentation was confusing to me. You can remove my bug report.
Previous Comments:
------------------------------------------------------------------------
[2012-08-10 10:26:03] 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-08 08:30:03] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62775&edit=1