Table of Contents

This RFC is still in progress

Functional elements to work with aggregations

Abstract

A common pattern in PHP is iterating through a list of objects and executing certain methods of the objects. This is especially common when it comes to 1:n-relations (e.g. one object, n adapters). This proposal is meant to abstract those iterations in convenient functional constructs by adding method call iterators and iterator functions.

Current usage patterns

UC-1: Iterating over an aggregation of objects

foreach ($list as $element) {
    $element->method();
}

UC-2: Iterating over an aggregation of objects and keeping the results

$result = array();
foreach ($list as $element) {
    $result[] = $element->method();
}

UC-3: Iterating over an aggregation of objects and conditionally executing a method

foreach ($list as $element) {
    if ($element->conditionalMethod()) {
        $element->method();
    }
}

UC-4: Iterating over an aggregation of objects and conditionally executing a method while keeping the results

$result = array();
foreach ($list as $element) {
    if ($element->conditionalMethod()) {
        $result[] = $element->method();
    }
}

Required functionality

Extracting the required functionality to leads to the following required additions:

Modified use cases

UC-1: Iterating over an aggregation of objects

iterate(new CallIterator($list, 'method'));

UC-2: Iterating over an aggregation of objects and keeping the results

$result = iterate(new CallIterator($list, 'method'), true);