Bug #75692 [Com]: Changing properties of cloned object can alter the original

From: Date: Fri, 15 Dec 2017 15:44:52 +0000
Subject: Bug #75692 [Com]: Changing properties of cloned object can alter the original
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-213119@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=75692&edit=1 ID: 75692 Comment by: danack@php.net Reported by: sdaicz at dc dot uba dot ar Summary: Changing properties of cloned object can alter the original Status: Open Type: Bug Package: Scripting Engine problem Operating System: All PHP Version: 7.0.26 Block user comment: N Private report: N New Comment: > This seems to happen only in php 7.0. > php 7.1 and 5.6 both work as expected. The behaviour appears right not only in 7.1 but in 7.2. PHP 7.0 had "Active Support Until 12 days ago" - also that behaviour is unlikely to be fixed in point version. Previous Comments: ------------------------------------------------------------------------ [2017-12-15 15:38:39] sdaicz at dc dot uba dot ar Description: ------------ - Clone an object - Iterate by reference through the attribute values of either the original or the clone - Change attribute values inside the loop - Both objects get their attribute values changed instead of only the one intended Tested on Windows and Linux. This seems to happen only in php 7.0. php 7.1 and 5.6 both work as expected. Test script: --------------- $a = new \stdClass(); $a->b = 1; $a->c = 2; $d = clone $a; foreach ($d as $key=>&$value) { $value += 10; } unset($value); var_dump($a); var_dump($d); Expected result: ---------------- Object $a should keep its original values ("b"=>1, "c"=>2). This is what happens in 7.1 and 5.6: class stdClass#1 (2) { public $b => int(1) public $c => int(2) } class stdClass#2 (2) { public $b => int(11) public $c => int(12) } Actual result: -------------- Object $a is altered by the loop, resulting in the same values as $d object(stdClass)#1 (2) { ["b"]=> int(11) ["c"]=> int(12) } object(stdClass)#2 (2) { ["b"]=> int(11) ["c"]=> int(12) } ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=75692&edit=1

« previous php.bugs (#213119) next »