#35663 [Opn->Bgs]: Assigning object to new array item overwrites existing items
| From: | tony2001@php.net | Date: | Tue, 13 Dec 2005 21:57:08 +0000 |
| Subject: | #35663 [Opn->Bgs]: Assigning object to new array item overwrites existing items | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-90398@lists.php.net to get a copy of this message | ||
ID: 35663
Updated by: tony2001@php.net
Reported By: jiri dot fogl at flyweb dot cz
-Status: Open
+Status: Bogus
Bug Type: Arrays related
Operating System: Linux, Windows
PHP Version: 5.1.1
New Comment:
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
"When assigning an already created instance of an object to a new
variable, the new variable will access the same instance as the object
that was assigned. This behaviour is the same when passing instances to
a function. A new instance of an already created object can be made by
cloning it."
http://www.php.net/manual/en/language.oop5.basic.php
Previous Comments:
------------------------------------------------------------------------
[2005-12-13 22:53:49] jiri dot fogl at flyweb dot cz
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 this bug report at http://bugs.php.net/?id=35663&edit=1