The yield from expression forwards yields from another iterable and can return a nested generator return value.
The examples below begin with the core behavior, then move to an application-oriented use.
How PHP Generators: yield from and Delegation Works
The yield from expression forwards yields from another iterable and can return a nested generator return value.
Example 1: Basic Usage
This example uses a small input to make the main behavior visible.
<?php
function primaryData(): Generator { yield "one"; yield "two"; }
function allData(): Generator { yield from primaryData(); yield "three"; }
foreach (allData() as $item) { echo $item . " "; }
Output:
one two three
The yield from expression forwards yields from another iterable and can return a nested generator return value.
Example 2: A Different Input
A different set of values shows the same operation in another situation.
<?php
function evenValues(): Generator { yield from [2, 4, 6]; }
function allValues(): Generator { yield 0; yield from evenValues(); }
print_r(iterator_to_array(allValues()));
Output:
Array
(
[0] => 2
[1] => 4
[2] => 6
)
yield from inherits key behavior from the nested iterable; keys may conflict when collecting results.
Practical Example: Applying the Feature
This example shows an application-style use of the feature, with its surrounding setup visible.
<?php
function headerRows(): Generator { yield "name,price"; }
function dataRows(): Generator { yield "book,12"; yield "pen,3"; }
function allRows(): Generator { yield from headerRows(); yield from dataRows(); }
foreach (allRows() as $csvRow) { echo $csvRow . "\n"; }
Output:
name,price
book,12
pen,3
The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.
Important Behavior and Edge Cases
- yield from inherits key behavior from the nested iterable; keys may conflict when collecting results.
- The yield from expression forwards yields from another iterable and can return a nested generator return value.
- Observe differences in empty inputs, invalid states and optional extension availability where relevant.
Comparison With Related Approaches
| Approach | Important Difference |
|---|---|
PHP Generators: yield from and Delegation | The yield from expression forwards yields from another iterable and can return a nested generator return value. |
| Alternative approach | Nested foreach with yield offers explicit control when you need to transform delegated values. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of PHP Generators: yield from and Delegation | yield from inherits key behavior from the nested iterable; keys may conflict when collecting results. |
| Expecting a different type of result | Read the declared return type and distinguish returned values from changes to the source object. |
| Assuming a related API works identically | Nested foreach with yield offers explicit control when you need to transform delegated values. |
Key Takeaways
- The yield from expression forwards yields from another iterable and can return a nested generator return value.
- yield from inherits key behavior from the nested iterable; keys may conflict when collecting results.
- Use the example that matches the values, runtime version and dependencies in your application.
PHP Generators: yield from and Delegation is useful when its behavior matches the data structure and lifecycle of your PHP application. Choose it with a clear understanding of its preconditions and returned values.