September 2017
Intermediate to advanced
244 pages
6h 44m
English
As functions can call each other, similarly a generator can also delegate to another generator. Here is how a generator delegates:
<?phpfunction gen(){ yield "yield 1 from gen1"; yield "yield 2 from gen1"; yield from gen2();}function gen2(){ yield "yield 1 from gen2"; yield "yield 2 from gen2";}foreach (gen() as $val){ echo $val, PHP_EOL;}/* above will result in output:yield 1 from gen1yield 2 from gen1yield 1 from gen2yield 2 from gen2 */
Here, gen2() is another generator being called in gen(), so a third yield in gen(), that is yield from gen2();, will transfer control to gen2(). So with that, it will start using yield from gen2().
Read now
Unlock full access