Bug #71009 [Opn->Nab]: A reference references multiple variables after clone.

From: Date: Tue, 01 Dec 2015 15:57:49 +0000
Subject: Bug #71009 [Opn->Nab]: A reference references multiple variables after clone.
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197547@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71009&edit=1 ID: 71009 Updated by: leigh@php.net Reported by: remyfox at hotmail dot com Summary: A reference references multiple variables after clone. -Status: Open +Status: Not a bug Type: Bug Package: Class/Object related Operating System: Windows PHP Version: 5.6.16 Block user comment: N Private report: N New Comment: Hi, This is intentional behaviour and is documented in the manual. http://php.net/manual/en/language.oop5.cloning.php "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." $z = &$a1->z; makes both sides of the assignment references to the same value, and since $a1->z is now a reference it is cloned as a reference. Previous Comments: ------------------------------------------------------------------------ [2015-12-01 14:24:31] remyfox at hotmail dot com Description: ------------ The values that are printed are both 11 and 12. I doubt this is correct behaviour. Test script: --------------- class A { public $z = 10; } $a1 = new A(); $z = &$a1->z; $a2 = clone $a1; $a1->z = 11; var_dump($z); $a2->z = 12; var_dump($z); ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=71009&edit=1

« previous php.bugs (#197547) next »