Generator pipelines for processing large datasets without memory issues
Processing a CSV with 500k rows? Loading it into an array first will hit memory limits. Generators let you process one row at a time with constant memory usage regardless of file size.
Here is a complete pipeline pattern you can run in sandbox (using an in-memory string instead of an actual file):
The entire pipeline processes one row at a time. Peak memory stays flat no matter how large the input gets.
Great pattern. You can generalize the pipeline construction to avoid the deeply nested call syntax:
One thing to keep in mind: generators are not rewindable. Once you consume a generator you cannot iterate it again. If you need to pass the same data through multiple consumers, collect it into an array first or wrap it in a class that re-creates the generator on each iteration.
Also, exception handling inside generators can be tricky. If an exception is thrown inside yield, the generator terminates and the exception propagates to the foreach call site. Make sure your pipeline handles errors at the consumer level with try/catch around the foreach, not inside individual generators.
```php blocks are runnable.