Bug #70978 [NEW]: reset(), current() result for objects implementing Iterator is bogus

From: Date: Thu, 26 Nov 2015 15:01:44 +0000
Subject: Bug #70978 [NEW]: reset(), current() result for objects implementing Iterator is bogus
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-197434@lists.php.net to get a copy of this message
From: danack Operating system: N/A PHP version: 7.0.0RC8 Package: Scripting Engine problem Bug Type: Bug Bug description:reset(), current() result for objects implementing Iterator is bogus 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 bug report at https://bugs.php.net/bug.php?id=70978&edit=1 -- Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70978&r=trysnapshot54 Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70978&r=trysnapshot55 Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70978&r=trysnapshottrunk Fixed in SVN: https://bugs.php.net/fix.php?id=70978&r=fixed Fixed in release: https://bugs.php.net/fix.php?id=70978&r=alreadyfixed Need backtrace: https://bugs.php.net/fix.php?id=70978&r=needtrace Need Reproduce Script: https://bugs.php.net/fix.php?id=70978&r=needscript Try newer version: https://bugs.php.net/fix.php?id=70978&r=oldversion Not developer issue: https://bugs.php.net/fix.php?id=70978&r=support Expected behavior: https://bugs.php.net/fix.php?id=70978&r=notwrong Not enough info: https://bugs.php.net/fix.php?id=70978&r=notenoughinfo Submitted twice: https://bugs.php.net/fix.php?id=70978&r=submittedtwice register_globals: https://bugs.php.net/fix.php?id=70978&r=globals PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70978&r=php4 Daylight Savings: https://bugs.php.net/fix.php?id=70978&r=dst IIS Stability: https://bugs.php.net/fix.php?id=70978&r=isapi Install GNU Sed: https://bugs.php.net/fix.php?id=70978&r=gnused Floating point limitations: https://bugs.php.net/fix.php?id=70978&r=float No Zend Extensions: https://bugs.php.net/fix.php?id=70978&r=nozend MySQL Configuration Error: https://bugs.php.net/fix.php?id=70978&r=mysqlcfg

« previous php.bugs (#197434) next »