1--TEST-- 2yield from on multiple trees needing merge 3--FILE-- 4<?php 5 6function from($levels) { 7 foreach (range(0, 2 << $levels) as $v) { 8 yield $v; 9 if ($v == (1 << ($levels - 1)) - 2) { 10 throw new Exception(); 11 } 12 } 13} 14 15function gen($gen, $level) { 16 yield from $gen; 17} 18 19$levels = 5; 20 21print "$levels levels\n\n"; 22 23$all = array(); 24$all[] = $gens[0][0] = from($levels); 25 26for ($level = 1; $level < $levels; $level++) { 27 for ($i = 0; $i < (1 << $level); $i++) { 28 $all[] = $gens[$level][$i] = gen($gens[$level-1][$i >> 1], $level); 29 } 30} 31 32for ($i = 0; $i < 2; $i++) { 33 try { 34 foreach ($all as $gen) { 35 var_dump($gen->current()); 36 $gen->next(); 37 if (!$gen->valid()) { 38 break; 39 } 40 } 41 } catch(Exception $e) { 42 print "$e\n"; 43 unset($all[array_search($gen, $all)]); 44 } 45} 46?> 47--EXPECTF-- 485 levels 49 50int(0) 51int(1) 52int(2) 53int(3) 54int(4) 55int(5) 56int(6) 57int(7) 58int(8) 59int(9) 60int(10) 61int(11) 62int(12) 63int(13) 64int(14) 65Exception in %s:%d 66Stack trace: 67#0 %s(%d): from(5) 68#1 %s(%d): gen(Object(Generator), 1) 69#2 %s(%d): gen(Object(Generator), 2) 70#3 [internal function]: gen(Object(Generator), 3) 71#4 %s(%d): Generator->next() 72#5 {main} 73ClosedGeneratorException: Generator yielded from aborted, no return value available in %s:%d 74Stack trace: 75#0 [internal function]: gen(Object(Generator), 1) 76#1 %s(%d): Generator->current() 77#2 {main} 78