|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2001-08-10 13:21 UTC] metz at studenten dot net
When filling an array with reference to objects the current() function does not return the reference to the object but a copy of the object. i.e. $ref = ¤t($some_array); returns a copy work around: $ref = &$some_array[key($some_array)]; returns the reference Is this a bug or a difference in implementation? PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Oct 24 13:00:02 2025 UTC |
test script: // Class definiton used for reference testing class Test { var $t = "x"; function Test(){} function get_t() { return($this->t); } function set_t($t) { $this->t = $t; } } // Array container reference test $ref_one = new Test(); $ref_two = new Test(); $ref_three = new Test(); $ref_one->t = "one"; $ref_two->t = "two"; $ref_three->t = "three"; $refs = array(); $refs["one"] = &$ref_one; $refs["two"] = &$ref_two; $refs["three"] = &$ref_three; print("<B>Test 7-1: (start)</B><BR>\n"); reset($refs); for($idx = 0; $idx < count($refs); $idx++) { $key = key($refs); $tmp = &$refs[$key]; print("${key} => " . $tmp->get_t() . "<BR>\n"); next($refs); } print("<BR>\n"); print("<B>Test 7-2: (using &current() to adjust)</B><BR>\n"); reset($refs); for($idx = 0; $idx < count($refs); $idx++) { $key = key($refs); $tmp = ¤t($refs); print("Setting: ${key} => ${idx}<BR>\n"); $tmp->set_t($idx); next($refs); } reset($refs); print("<B>Result:</B><BR>\n"); for($idx = 0; $idx < count($refs); $idx++) { $key = key($refs); $tmp = &$refs[$key]; print("${key} => " . $tmp->get_t() . "<BR>\n"); next($refs); } print("<BR>\n"); print("<B>Test 7-3: (using direct method to adjust)</B><BR>\n"); reset($refs); for($idx = 0; $idx < count($refs); $idx++) { $key = key($refs); $tmp = &$refs[$key]; print("Setting: ${key} => ${idx}<BR>\n"); $tmp->set_t($idx); next($refs); } reset($refs); print("<B>Result:</B><BR>\n"); for($idx = 0; $idx < count($refs); $idx++) { $key = key($refs); $tmp = &$refs[$key]; print("${key} => " . $tmp->get_t() . "<BR>\n"); next($refs); } print("<BR>\n");