PMG Digital Made for Humans

Generators & Iterator Aggregate: A Match Made in PHP Heaven

2 MINUTE READ | January 26, 2016

Generators & Iterator Aggregate: A Match Made in PHP Heaven

Author's headshot

Christopher Davis

Christopher Davis has written this article. More details coming soon.

iterator-aggregate-generator

PHP added generators in version 5.5. You see them in code with the yield keyword and they tend to be used most often to iterate over a collection dynamically without creating it in memory all at once. Really you can do all sorts of fun things with generators that we’re not going to touch here.

IteratorAggregate has been around forever — since PHP 5.0. It’s a way to make an object traversable (able to be used in a foreach loop) without having to implement all the methods of Iterator.

IteratorAggregate
also makes it possible to do nested loops on the same object.

A powerful combination is to use a generator within your IteratorAggregate::getIterator implementation. Rather than returning a new iterator object each time, just supply a generator. Here’s a small example.

MultipyingIterator.php

<?phpclass MultiplyingIterator implements \IteratorAggregate{    /**     * @var array|Traversable     */    private $wrapped;    private $factor;    public function __construct($wrapped, $factor)    {        // will break if wrapped is a generator        // in the real world we'd type check this        $this->wrapped = $wrapped;        $this->factor = $factor;    }    public function getIterator()    {        foreach ($this->wrapped as $item) {            yield $item => $item * $this->factor;        }    }}

And it’s usage:

example.php

<?php$iter = new MultiplyingIterator([1, 2, 3, 4], 2);foreach ($iter as $orig => $changed) {    echo $orig, ': ', $changed, PHP_EOL;    foreach ($iter as $orig => $changed) {        echo '  inner ', $orig, ': ', $changed, PHP_EOL;    }}

This is an extremely powerful pattern when you need to iterate over collections with some form of known computation mixed in. Our recently released CSV library does this to produce associative arrays from a CSV file.

Something more dynamic would be better suited to a generic map implementation.

map.php

Stay in touch

Bringing news to you

Subscribe to our newsletter

By clicking and subscribing, you agree to our Terms of Service and Privacy Policy

<?phpfunction map(callable $cb, $collection) {    foreach ($collection as $key => $item) {        yield $item => $cb($item);    }}