|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2014-08-11 10:34 UTC] thekid@php.net
Description:
------------
A generator can be rewound if and only if its first yielded value has been fetched. As soon as we fetch the second (or any further) yielded value and *then* try to rewind it we actually get the exception. This is inconsistent.
Test script:
---------------
$f= function() { yield 1; yield 2; yield 3; };
$gen= $f();
foreach ($gen as $val) { var_dump($val); }
foreach ($gen as $val) { var_dump($val); } // Exception("Cannot rewind a generator that was already run")
# Here's the bug:
foreach ($gen as $val) { var_dump($val); break; }
foreach ($gen as $val) { var_dump($val); } // No exception
Expected result:
----------------
int(1)
Exception("Cannot rewind a generator that was already run")
Actual result:
--------------
int(1)
int(1)
int(2)
int(3)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2026 The PHP GroupAll rights reserved. |
Last updated: Wed Jan 07 01:00:01 2026 UTC |
rewind() is possible at first yield to support the standard Iterator pattern: for ($it->rewind(); $it->valid(); $it->next()) { // ... } In which case the rewind is a no-op, priming notwithstanding.