php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37153 foreach on array of object, object is referenced not copied
Submitted: 2006-04-21 13:33 UTC Modified: 2006-04-21 13:41 UTC
From: sirber at webernic dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.1.2 OS: Linuix 2.6.13
Private report: No CVE-ID: None
 [2006-04-21 13:33 UTC] 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"
  }
}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-04-21 13:39 UTC] 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:41 UTC] mike@php.net
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.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 29 23:00:03 2025 UTC