|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2017-12-15 15:44 UTC] danack@php.net
[2020-10-29 17:26 UTC] cmb@php.net
-Status: Open
+Status: Closed
-Assigned To:
+Assigned To: cmb
[2020-10-29 17:26 UTC] cmb@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 16 04:00:02 2025 UTC |
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) }