#37384 [Opn->Bgs]: Object Scope within Flow Structures
| From: | helly@php.net | Date: | Tue, 09 May 2006 18:12:47 +0000 |
| Subject: | #37384 [Opn->Bgs]: Object Scope within Flow Structures | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-96864@lists.php.net to get a copy of this message | ||
ID: 37384
Updated by: helly@php.net
Reported By: Anthony dot Cashaw at RoswellPark dot org
-Status: Open
+Status: Bogus
Bug Type: *General Issues
-Operating System: Windows XP and Linux
+Operating System: *
-PHP Version: 5.1.4
+PHP Version: *
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
Previous Comments:
------------------------------------------------------------------------
[2006-05-09 15:59:10] Anthony dot Cashaw at RoswellPark dot org
Description:
------------
Should you need to nullify an object created on the fly in the scope of
a (for lack of general term) loop when adding those objects to an array?
foreach and while were tested. I tested even objects that I defined
with the class keyword and when the object was instantiated outside of
the loop the values that printed where the same at each index.
I'm of the mind that this is how objects work since they're really
pointers to a memory address, but then why would the address assigment
operator ( &= ) be usefull with objects in cases like this?
Reproduce code:
---------------
<?php
$array = array("one" => 1, "two" => 2, "three" => 3);
foreach($array as $key => $value){
$MyObject->key = $key;
$MyObject->value = $value;
$free[] = $MyObject;
//unless I uncomment the next line there are issues
//$MyObject= null;
}
echo "<pre>";
print_r($free);
echo "</pre>";
?>
Expected result:
----------------
Array
(
[0] => stdClass Object
(
[key] => one
[value] => 1
)
[1] => stdClass Object
(
[key] => two
[value] => 2
)
[2] => stdClass Object
(
[key] => three
[value] => 3
)
)
Actual result:
--------------
Array
(
[0] => stdClass Object
(
[key] => three
[value] => 3
)
[1] => stdClass Object
(
[key] => three
[value] => 3
)
[2] => stdClass Object
(
[key] => three
[value] => 3
)
)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=37384&edit=1