Content · Programming · PHP

PHP Generator Delegation with yield from

PHP Generator Delegation with yield from: yield from delegates a generator or iterable to another iterable while preserving the sequence of yielded values. See three examples and important limitations.

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

yield from delegates a generator or iterable to another iterable while preserving the sequence of yielded values.

The examples below begin with the core behavior, then move to an application-oriented use.

How PHP Generator Delegation with yield from Works


yield from delegates a generator or iterable to another iterable while preserving the sequence of yielded values.

Example 1: Basic Usage


This example uses a small input to make the main behavior visible.

<?php

function leftPart(): Generator { yield 1; yield 2; }
function allParts(): Generator { yield from leftPart(); yield 3; }
foreach (allParts() as $value) { echo $value; }

Output:

123

yield from delegates a generator or iterable to another iterable while preserving the sequence of yielded values.

Example 2: A Different Input


A different set of values shows the same operation in another situation.

<?php

function names(): Generator { yield from ["Alex", "Sam"]; }
foreach (names() as $name) { echo $name . " "; }

Output:

Alex Sam 

Duplicate keys can overwrite entries if results are converted to an array with key preservation.

Practical Example: Applying the Feature


This example shows an application-style use of the feature, with its surrounding setup visible.

<?php

function parts(): Generator { yield from ["A", "B"]; }
function combined(): Generator { yield "start"; yield from parts(); }
foreach (combined() as $part) { echo $part; }

Output:

startAB

The code is shown for learning. Adjust the class names, inputs and error handling to suit your application.

Important Behavior and Edge Cases


  • Duplicate keys can overwrite entries if results are converted to an array with key preservation.
  • yield from delegates a generator or iterable to another iterable while preserving the sequence of yielded values.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
PHP Generator Delegation with yield fromyield from delegates a generator or iterable to another iterable while preserving the sequence of yielded values.
Alternative approachManually yielding every child item is equivalent but more verbose.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of PHP Generator Delegation with yield fromDuplicate keys can overwrite entries if results are converted to an array with key preservation.
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 identicallyManually yielding every child item is equivalent but more verbose.

Key Takeaways


  • yield from delegates a generator or iterable to another iterable while preserving the sequence of yielded values.
  • Duplicate keys can overwrite entries if results are converted to an array with key preservation.
  • Use the example that matches the values, runtime version and dependencies in your application.

PHP Generator Delegation with yield from 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.