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
| Approach | Important Difference |
|---|---|
Implementing Lazy Iteration with Generators | A generator yields items on demand, which is useful when creating or reading large sequences. |
| Alternative approach | Building an array holds all values in memory; yielding them can avoid that allocation. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
Ignoring the documented state or precondition of Implementing Lazy Iteration with Generators | Yielding values does not by itself make source reads nonblocking, and a consumed generator cannot normally restart. |
| 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 | Building 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.