Bug #69677 [Nab]: External attribute specifies a reference to an object, clone the original objec

From: Date: Mon, 25 May 2015 03:40:11 +0000
Subject: Bug #69677 [Nab]: External attribute specifies a reference to an object, clone the original objec
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-192877@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=69677&edit=1 ID: 69677 User updated by: ttlxihuan at 163 dot com Reported by: ttlxihuan at 163 dot com Summary: External attribute specifies a reference to an object, clone the original objec Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: window,linux PHP Version: 5.6.9 Assigned To: cmb Block user comment: N Private report: N New Comment: I may express not very good, I think the most surprising is "$n = &$b->n;" phrase program, supposedly "$b->n" should not be a reference variable, "$n" is cited variable, but after clone "$b" to modify the clone "$cb->n = 'clone';", the original "$cb->n" should not be a reference, it should be "$b->n" replica, but modifying the properties of clone has affected the value of "$b->n" is. The same operation "$N = &$this->n;" in the constructor __construct then clone, clone modify properties of an object does not affect the properties of the original object. Is it because the object attribute is an external reference, as long as the reference variable life cycle after changing the clone will change the properties of an object as a reference? Previous Comments: ------------------------------------------------------------------------ [2015-05-24 22:21:38] cmb@php.net 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> ------------------------------------------------------------------------ [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

« previous php.bugs (#192877) next »