Bug #69677 [Opn->Nab]: External attribute specifies a reference to an object, clone the original objec
Edit report at https://bugs.php.net/bug.php?id=69677&edit=1
ID: 69677
Updated by: cmb@php.net
Reported by: ttlxihuan at 163 dot com
Summary: External attribute specifies a reference to an
object, clone the original objec
-Status: Open
+Status: Not a bug
Type: Bug
Package: Scripting Engine problem
Operating System: window,linux
PHP Version: 5.6.9
-Assigned To:
+Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
The manual page on object cloning[1] clarifies the issue:
| 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.
If you slightly modify your test script, so that the reference to
A::$n is preserved, you get the same result:
<http://3v4l.org/NRTJZ>.
[1] <http://php.net/manual/en/language.oop5.cloning.php>
Previous Comments:
------------------------------------------------------------------------
[2015-05-21 07:10:21] ttlxihuan at 163 dot com
Description:
------------
class a{
public $n;
public function __construct($n)
{
$this->n=$n;
$N=&$this->n;
}
}
class b{
public $n;
public function __construct($n)
{
$this->n=$n;
}
}
class c{
public function t($b){
$cb= clone $b;
$cb->n='clone';
var_dump($cb->n,$b->n);
}
}
Test script:
---------------
$c=new c();
$a=new a('new');
$b=new b('new');
$n=&$b->n;
//Class A specified reference when constructing, clone and modify the properties of the original
object has no effect
$c->t($a);
//Class B specified external references, clone modify properties directly affect the original object
properties
$c->t($b);
Expected result:
----------------
I may be wrong envisioned, I believe that adding a reference to the cloned object should be
completely independent and does not affect the original object property values.The end result should
be:
string(5) "clone"
string(3) "new"
string(5) "clone"
string(5) "new"
Actual result:
--------------
#Result:
string(5) "clone"
string(3) "new"
string(5) "clone"
string(5) "clone"
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69677&edit=1
Thread (4 messages)