Bug #70978 [Opn->Nab]: reset(), current() result for objects implementing Iterator is bogus

From: Date: Thu, 26 Nov 2015 15:14:23 +0000
Subject: Bug #70978 [Opn->Nab]: reset(), current() result for objects implementing Iterator is bogus
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197435@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70978&edit=1 ID: 70978 Updated by: danack@php.net Reported by: danack@php.net Summary: reset(), current() result for objects implementing Iterator is bogus -Status: Open +Status: Not a bug Type: Bug Package: Scripting Engine problem Operating System: N/A PHP Version: 7.0.0RC8 Block user comment: N Private report: N New Comment: Thank you for taking the time to write to us, but this is not a bug. Please double-check the documentation available at http://www.php.net/manual/ and the instructions on how to report a bug at http://bugs.php.net/how-to-report.php Apparently current() and reset() are not aware of \Iterator. And so the behaviour below is acceptable, if non-intuitive. Previous Comments: ------------------------------------------------------------------------ [2015-11-26 15:01:43] danack@php.net 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. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=70978&edit=1

« previous php.bugs (#197435) next »