Content · Programming · PHP

PHP Generators: yield from and Delegation

PHP Generators: yield from and Delegation: The yield from expression forwards yields from another iterable and can return a nested generator return value. See three examples and important limitations.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

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


ApproachImportant Difference
PHP Generators: yield from and DelegationThe yield from expression forwards yields from another iterable and can return a nested generator return value.
Alternative approachNested foreach with yield offers explicit control when you need to transform delegated values.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP Generators: yield from and Delegationyield from inherits key behavior from the nested iterable; keys may conflict when collecting results.
Expecting a different type of resultRead the declared return type and distinguish returned values from changes to the source object.
Assuming a related API works identicallyNested 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.

Share this resourceSend this page to someone who may find it useful.