Content · Programming · PHP

Building an Iterable Pipeline with PHP Generators

Building an Iterable Pipeline with PHP Generators: A generator pipeline composes producers, filtering stages and consumers while producing one value at a time. See three examples and important limitations.

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

A generator pipeline composes producers, filtering stages and consumers while producing one value at a time.

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

How Building an Iterable Pipeline with PHP Generators Works


A generator pipeline composes producers, filtering stages and consumers while producing one value at a time.

Example 1: Basic Usage


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

<?php

function values(): Generator { yield 2; yield 7; yield 12; }
$iterator = values();
foreach ($iterator as $value) { echo ($value * 2) . "\n"; }

Output:

4
14
24

A generator pipeline composes producers, filtering stages and consumers while producing one value at a time.

Example 2: A Different Input


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

<?php

function selectedValues(iterable $values): Generator { foreach ($values as $value) { if ($value >= 10) { yield $value; } } }
foreach (selectedValues([4, 10, 16]) as $value) { echo $value . " "; }

Output:

10 16 

Each generator can usually be traversed only once; preserve keys intentionally and avoid exhausting it before the final consumer.

Practical Example: Applying the Feature


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

<?php

function orders(): Generator { yield ["number" => 101, "total" => 45]; yield ["number" => 102, "total" => 12]; }
function expensiveOrders(iterable $orders): Generator { foreach ($orders as $order) { if ($order["total"] > 20) { yield $order["number"]; } } }
foreach (expensiveOrders(orders()) as $orderNumber) { echo $orderNumber; }

Output:

101

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

Important Behavior and Edge Cases


  • Each generator can usually be traversed only once; preserve keys intentionally and avoid exhausting it before the final consumer.
  • A generator pipeline composes producers, filtering stages and consumers while producing one value at a time.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
Building an Iterable Pipeline with PHP GeneratorsA generator pipeline composes producers, filtering stages and consumers while producing one value at a time.
Alternative approachAn eager array_filter/array_map pipeline allocates intermediate arrays.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of Building an Iterable Pipeline with PHP GeneratorsEach generator can usually be traversed only once; preserve keys intentionally and avoid exhausting it before the final consumer.
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 identicallyAn eager array_filter/array_map pipeline allocates intermediate arrays.

Key Takeaways


  • A generator pipeline composes producers, filtering stages and consumers while producing one value at a time.
  • Each generator can usually be traversed only once; preserve keys intentionally and avoid exhausting it before the final consumer.
  • Use the example that matches the values, runtime version and dependencies in your application.

Building an Iterable Pipeline with PHP Generators 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.