From: Anthony dot Cashaw at RoswellPark dot org Operating system: Windows XP and Linux PHP version: 5.1.4 PHP Bug Type: *General Issues Bug description: Object Scope within Flow Structures
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 bug report at http://bugs.php.net/?id=37384&edit=1 -- Try a CVS snapshot (PHP 4.4): http://bugs.php.net/fix.php?id=37384&r=trysnapshot44 Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=37384&r=trysnapshot52 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=37384&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=37384&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=37384&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=37384&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=37384&r=needscript Try newer version: http://bugs.php.net/fix.php?id=37384&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=37384&r=support Expected behavior: http://bugs.php.net/fix.php?id=37384&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=37384&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=37384&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=37384&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=37384&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=37384&r=dst IIS Stability: http://bugs.php.net/fix.php?id=37384&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=37384&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=37384&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=37384&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=37384&r=mysqlcfg