Generator - from
Let's see how to connect from one generator to another.
This is called generator delegation. Use the from keyword.
Generator from How to use
function generator() { echo "generator 1 operation 1 <br>"; echo "generator 1 operation 2 <br>"; yield "generator 1 - 1"; echo "generator 1 operation 3 <br>"; echo "generator 1 operation 4 <br>"; yield "generator 2 - 1"; echo "generator 1 operation 5 <br>"; echo "generator 1 operation 6 <br>"; yield from generator2(); } function generator2() { echo "generator 2 operation 1"; echo "generator 2 operation 2"; yield "generator 2 - 1"; return; }
Looking at the end of generator 1 above, we call generator 2 using the from keyword.
Now let's call Generator 1 and see that Generator 2 is also called.
<?php function generatorOne() { echo "generator 1 operation 1 <br>"; echo "generator 1 operation 2 <br>"; yield "generator 1 - 1<br>"; echo "generator 1 operation 3 <br>"; echo "generator 1 operation 4 <br>"; yield "generator 2 - 1<br>"; echo "generator 1 operation 5 <br>"; echo "generator 1 operation 6 <br>"; yield from generatorTwo(); } function generatorTwo() { echo "<hr>generator 2 operation 1<br>"; echo "generator 2 operation 2<br>"; yield "generator 2 - 1<br>"; return; } foreach(generatorOne() as $go){ echo $go; } ?>
Here is the result of the above code:
You learned how to call another generator from a generator using the from keyword. ^^