1--TEST-- 2A generator can only be rewinded before or at the first yield 3--FILE-- 4<?php 5 6function gen() { 7 echo "before yield\n"; 8 yield; 9 echo "after yield\n"; 10 yield; 11} 12 13$gen = gen(); 14$gen->rewind(); 15$gen->rewind(); 16$gen->next(); 17 18try { 19 $gen->rewind(); 20} catch (Exception $e) { 21 echo "\n", $e, "\n\n"; 22} 23 24function &gen2() { 25 $foo = 'bar'; 26 yield $foo; 27 yield $foo; 28} 29 30$gen = gen2(); 31foreach ($gen as $v) { } 32try { 33 foreach ($gen as $v) { } 34} catch (Exception $e) { 35 echo $e, "\n\n"; 36} 37 38function gen3() { 39 echo "in generator\n"; 40 41 if (false) yield; 42} 43 44$gen = gen3(); 45$gen->rewind(); 46 47?> 48--EXPECTF-- 49before yield 50after yield 51 52exception 'Exception' with message 'Cannot rewind a generator that was already run' in %s:%d 53Stack trace: 54#0 %s(%d): Generator->rewind() 55#1 {main} 56 57exception 'Exception' with message 'Cannot traverse an already closed generator' in %s:%d 58Stack trace: 59#0 %s(%d): unknown() 60#1 {main} 61 62in generator 63