Bug #12692 Updated: The current() function does not return references

From: Date: Fri, 10 Aug 2001 19:57:42 +0000
Subject: Bug #12692 Updated: The current() function does not return references
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-62871@lists.php.net to get a copy of this message
ID: 12692 Updated by: jeroen Reported By: metz@studenten.net Old Status: Open Status: Bogus Bug Type: Arrays related Operating System: Linux 2.4.2 PHP Version: 4.0.6 New Comment: That's intentional Previous Comments: ------------------------------------------------------------------------ [2001-08-10 13:27:43] metz@studenten.net 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 &amp;current() to adjust)</B><BR>\n"); reset($refs); for($idx = 0; $idx < count($refs); $idx++) { $key = key($refs); $tmp = &current($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"); ------------------------------------------------------------------------ [2001-08-10 13:21:25] metz@studenten.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 = &current($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? ------------------------------------------------------------------------ Edit this bug report at http://bugs.php.net/?id=12692&edit=1

« previous php.dev (#62871) next »