|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2015-11-26 15:14 UTC] danack@php.net
-Status: Open
+Status: Not a bug
[2015-11-26 15:14 UTC] danack@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 08 09:00:02 2026 UTC |
Description: ------------ When calling reset() or current() on an object implementing an \Iterator, the return value of these functions is bogus. Those functions are each meant to return the value, with current() returning the current value, and reset returning the value of the current element aka the first element. Instead, for objects implementing \Iterator, the value returned is that of the current key. Test script: --------------- <?php class myIterator implements Iterator { private $position = 0; private $array = array( "firstelement_Iterator", "secondelement_Iterator", "lastelement_Iterator", ); public function __construct() { $this->position = 0; } function rewind() { $this->position = 0; } function current() { return $this->array[$this->position]; } function key() { return $this->position; } function next() { ++$this->position; } function valid() { return isset($this->array[$this->position]); } } $tests = []; $test0 = [ "firstelement_Array", "secondelement_Array", "lastelement_Array", ]; $test1 = new ArrayIterator([ "firstelement_ArrayIterator", "secondelement_ArrayIterator", "lastelement_ArrayIterator", ]); $test2 = new myIterator(); $tests = [ 'array' => $test0, 'array_iterator' => $test1, 'Iterator' => $test2, ]; foreach ($tests as $testName => $test) { echo "start test $testName\n"; $current = current($test); $i = 0; $current = current($test); foreach ($test as $key => $value) { //echo "$key, $value \n"; } //current returns the value of the array element that's currently being pointed to by the // internal pointer. // If the internal pointer points beyond the end of the elements list or the array is empty, current() returns FALSE. var_dump(current($test)); // reset() rewinds array's internal pointer to the first element and // returns the value of the first array element - this should be firstelement_* var_dump(reset($test)); echo "End test.\n\n"; } Expected result: ---------------- start test array_iterator string(26) "firstelement_Iterator" string(26) "firstelement_Iterator" End test. Actual result: -------------- start test Iterator int(3) int(3) End test.