#37153 [Opn->Bgs]: foreach on array of object, object is referenced not copied

From: Date: Fri, 21 Apr 2006 13:41:21 +0000
Subject: #37153 [Opn->Bgs]: foreach on array of object, object is referenced not copied
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-96066@lists.php.net to get a copy of this message
ID: 37153 Updated by: mike@php.net Reported By: sirber at webernic dot com -Status: Open +Status: Bogus Bug Type: Unknown/Other Function Operating System: Linuix 2.6.13 PHP Version: 5.1.2 New Comment: Sorry, but your problem does not imply a bug in PHP itself. For a list of more appropriate places to ask for help using PHP, please visit http://www.php.net/support.php as this bug system is not the appropriate forum for asking support questions. Due to the volume of reports we can not explain in detail here why your report is not a bug. The support channels will be able to provide an explanation for you. Thank you for your interest in PHP. Objects are always passed by reference in PHP-5. Previous Comments: ------------------------------------------------------------------------ [2006-04-21 13:39:27] martin at webernic dot com Same result with a copy of the array. Source array and the copy are modified. Reproduce code: --------------- $aTmp = array(); $oTmp = new stdClass(); $oTmp->id = 1; $aTmp[] = $oTmp; $oTmp = new stdClass(); $oTmp->id = 2; $aTmp3 = $aTmp foreach ($aTmp3 as $iKey => $oTest) $oTest->id = 'tea'; var_dump($aTmp, $aTmp3); Expected result: ---------------- array(2) { [0]=> object(stdClass)#1 (1) { ["id"]=> int(1) } [1]=> object(stdClass)#2 (1) { ["id"]=> int(2) } } array(2) { [0]=> object(stdClass)#1 (1) { ["id"]=> int(1) } [1]=> object(stdClass)#2 (1) { ["id"]=> int(2) } } Actual result: -------------- array(2) { [0]=> object(stdClass)#1 (1) { ["id"]=> string(3) "tea" } [1]=> object(stdClass)#2 (1) { ["id"]=> string(3) "tea" } } array(2) { [0]=> object(stdClass)#1 (1) { ["id"]=> string(3) "tea" } [1]=> object(stdClass)#2 (1) { ["id"]=> string(3) "tea" } } ------------------------------------------------------------------------ [2006-04-21 13:33:10] sirber at webernic dot com Description: ------------ When you foreach an array of object, if you modify the object, the source is modified. PHP4: work as expected PHP5 with compatibility on: work as expected PHP5: source gets modified Reproduce code: --------------- $aTmp = array(); $oTmp = new stdClass(); $oTmp->id = 1; $aTmp[] = $oTmp; $oTmp = new stdClass(); $oTmp->id = 2; $aTmp[] = $oTmp; $oTmp = new stdClass(); $oTmp->id = 3; $aTmp[] = $oTmp; $oTmp = new stdClass(); $oTmp->id = 4; $aTmp[] = $oTmp; foreach ($aTmp as $iKey => $oTest) $oTest->id = 'tea'; var_dump($aTmp); Expected result: ---------------- array(4) { [0]=> object(stdClass)#1 (1) { ["id"]=> int(1) } [1]=> object(stdClass)#2 (1) { ["id"]=> int(2) } [2]=> object(stdClass)#3 (1) { ["id"]=> int(3) } [3]=> object(stdClass)#4 (1) { ["id"]=> int(4) } } Actual result: -------------- array(4) { [0]=> object(stdClass)#1 (1) { ["id"]=> string(3) "tea" } [1]=> object(stdClass)#2 (1) { ["id"]=> string(3) "tea" } [2]=> object(stdClass)#3 (1) { ["id"]=> string(3) "tea" } [3]=> object(stdClass)#4 (1) { ["id"]=> string(3) "tea" } } ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=37153&edit=1

« previous php.bugs (#96066) next »