|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2006-05-09 18:12 UTC] helly@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 16:00:01 2025 UTC |
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 ) )