#37153 [NEW]: foreach on array of object, object is referenced not copied
| From: | sirber at webernic dot com | Date: | Fri, 21 Apr 2006 13:33:10 +0000 |
| Subject: | #37153 [NEW]: foreach on array of object, object is referenced not copied | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-96063@lists.php.net to get a copy of this message | ||
From: sirber at webernic dot com
Operating system: Linuix 2.6.13
PHP version: 5.1.2
PHP Bug Type: Unknown/Other Function
Bug description: foreach on array of object, object is referenced not copied
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 bug report at http://bugs.php.net/?id=37153&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=37153&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=37153&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=37153&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=37153&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=37153&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=37153&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=37153&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=37153&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=37153&r=support
Expected behavior: http://bugs.php.net/fix.php?id=37153&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=37153&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=37153&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=37153&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=37153&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=37153&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=37153&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=37153&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=37153&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=37153&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=37153&r=mysqlcfg