rfc:functional-elements

This is an old revision of the document!


Functional elements for object lists

Abstract

A common pattern in PHP is iterating through a list of objects and executing certain methods of the function. 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.

Current usage patterns

UC-1

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

UC-2

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

UC-3

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

UC-4

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

Extracted functions

spl_object_list_call()

array|bool spl_iterator_element_call(
    array|Iterator $iterator,
    string|array $methods,
    array $params = array(),
    bool $collect = true
)
array|mixed spl_iterator_element_call_conditional(
    array|Iterator $iterator,
    string|array $methods,
    string|array $conditions,
    array $params = array(),
    array $condition_params = array(),
    bool $collect = true,
    bool $finite = true
)
rfc/functional-elements.1204769267.txt.gz · Last modified: 2017/09/22 13:28 (external edit)