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

From: Date: Tue, 26 May 2015 10:22:30 +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-192888@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
 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:             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 suggest you read the chapter on reference counting basics[1] in
the manual. As soon as a reference to variable is created (&
operator), is_ref becomes true. If all references are gone, is_ref
becomes false again. clone simply makes a shallow copy of an
object, and leaves is_ref alone. So when a property of a clone
where is_ref is true is modified, the property of the original
object is also modified (because they are actually the same
variable).

[1] <http://php.net/manual/en/features.gc.refcounting-basics.php>


Previous Comments:
------------------------------------------------------------------------
[2015-05-25 03:40:09] ttlxihuan at 163 dot com

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?

------------------------------------------------------------------------
[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


Thread (4 messages)

« previous php.bugs (#192888) next »