Content · Programming · PHP

Implementing Lazy Iteration with Generators

Implementing Lazy Iteration with Generators: A generator yields items on demand, which is useful when creating or reading large sequences. See three examples and important limitations.

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

A generator yields items on demand, which is useful when creating or reading large sequences.

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

How Implementing Lazy Iteration with Generators Works


A generator yields items on demand, which is useful when creating or reading large sequences.

Example 1: Basic Usage


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

<?php

function readIds(): Generator { foreach ([41, 42, 43] as $id) { yield $id; } }
$ids = readIds();
echo $ids->current();

Output:

41

A generator yields items on demand, which is useful when creating or reading large sequences.

Example 2: A Different Input


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

<?php

function squared(int $limit): Generator { for ($number = 1; $number <= $limit; ++$number) { yield $number * $number; } }
foreach (squared(4) as $value) { echo $value . " "; }

Output:

1 4 9 16 

Yielding values does not by itself make source reads nonblocking, and a consumed generator cannot normally restart.

Practical Example: Applying the Feature


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

<?php

function linesFromString(string $contents): Generator { foreach (explode("\n", $contents) as $line) { yield trim($line); } }
foreach (linesFromString("one\ntwo\nthree") as $line) { echo strtoupper($line) . "\n"; }

Output:

ONE
TWO
THREE

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

Important Behavior and Edge Cases


  • Yielding values does not by itself make source reads nonblocking, and a consumed generator cannot normally restart.
  • A generator yields items on demand, which is useful when creating or reading large sequences.
  • Observe differences in empty inputs, invalid states and optional extension availability where relevant.

Comparison With Related Approaches


ApproachImportant Difference
Implementing Lazy Iteration with GeneratorsA generator yields items on demand, which is useful when creating or reading large sequences.
Alternative approachBuilding an array holds all values in memory; yielding them can avoid that allocation.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring the documented state or precondition of Implementing Lazy Iteration with GeneratorsYielding values does not by itself make source reads nonblocking, and a consumed generator cannot normally restart.
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 identicallyBuilding an array holds all values in memory; yielding them can avoid that allocation.

Key Takeaways


  • A generator yields items on demand, which is useful when creating or reading large sequences.
  • Yielding values does not by itself make source reads nonblocking, and a consumed generator cannot normally restart.
  • Use the example that matches the values, runtime version and dependencies in your application.

Implementing Lazy Iteration with 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.