#35663 [NEW]: Assigning object to new array item overwrites existing items
| From: | jiri dot fogl at flyweb dot cz | Date: | Tue, 13 Dec 2005 21:53:50 +0000 |
| Subject: | #35663 [NEW]: Assigning object to new array item overwrites existing items | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-90397@lists.php.net to get a copy of this message | ||
From: jiri dot fogl at flyweb dot cz
Operating system: Linux, Windows
PHP version: 5.1.1
PHP Bug Type: Arrays related
Bug description: Assigning object to new array item overwrites existing items
Description:
------------
I wanted to have array of objects. After assigning an object variable to a
new array item (using bracket syntax $array[] = $object;), all items are
identical.
I found the bug in PHP 5.0.5/Linux and reproduced with 5.1.1/Windows. With
4.4.1 it works fine.
Direct assignment ($array[]->property = ...;) works.
Reproduce code:
---------------
<?php
// This overwrites items:
$array = Array();
$object->property = 1;
$array[] = $object;
$object->property = 2;
$array[] = $object;
print_r($array);
/* This works:
* $array = Array();
* $array[]->property = 1;
* $array[]->property = 2;
* print_r($array);
*/
?>
Expected result:
----------------
After running this code I expect this output:
Array
(
[0] => stdClass Object
(
[property] => 1
)
[1] => stdClass Object
(
[property] => 2
)
)
Actual result:
--------------
Array
(
[0] => stdClass Object
(
[property] => 2
)
[1] => stdClass Object
(
[property] => 2
)
)
--
Edit bug report at http://bugs.php.net/?id=35663&edit=1
--
Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=35663&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): http://bugs.php.net/fix.php?id=35663&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=35663&r=trysnapshot60
Fixed in CVS: http://bugs.php.net/fix.php?id=35663&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=35663&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=35663&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=35663&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=35663&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=35663&r=support
Expected behavior: http://bugs.php.net/fix.php?id=35663&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=35663&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=35663&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=35663&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=35663&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=35663&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=35663&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=35663&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=35663&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=35663&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=35663&r=mysqlcfg