rfc:functional-elements
Differences
This shows you the differences between two versions of the page.
rfc:functional-elements [2008/03/16 14:29] lstrojny Linking is_traversable() |
rfc:functional-elements [2017/09/22 13:28] |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | 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 === | ||
- | <code php> | ||
- | foreach ($list as $element) { | ||
- | $element->method(); | ||
- | } | ||
- | </code> | ||
- | |||
- | === UC-2: Iterating over an aggregation of objects and keeping the results === | ||
- | <code php> | ||
- | $result = array(); | ||
- | foreach ($list as $element) { | ||
- | $result[] = $element->method(); | ||
- | } | ||
- | </code> | ||
- | |||
- | === UC-3: Iterating over an aggregation of objects and conditionally executing a method === | ||
- | <code php> | ||
- | foreach ($list as $element) { | ||
- | if ($element->conditionalMethod()) { | ||
- | $element->method(); | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | === UC-4: Iterating over an aggregation of objects and conditionally executing a method while keeping the results === | ||
- | <code php> | ||
- | $result = array(); | ||
- | foreach ($list as $element) { | ||
- | if ($element->conditionalMethod()) { | ||
- | $result[] = $element->method(); | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | |||
- | ==== Required functionality ==== | ||
- | Extracting the required functionality to leads to the following required additions: | ||
- | * [[rfc:functional-elements:call-iterator|class CallIterator]]: a simple iterator which walks over a traversable list (including arrays) and calls a specified method. The CallIterator might optionally gather the results | ||
- | * [[rfc::functional-elements:is-traversable|function is_traversable()]]: Returns true if a list is traversable (Iterator/IteratorAggregate/array) | ||
- | * function iterate(): Walks over a traversable list and does nothing | ||
- | * function iterator_true(): Walks over a traversable list and assumes every element to be true | ||
- | * function iterator_false(): Walks over a traversable list and assumes every element to be false | ||
rfc/functional-elements.txt · Last modified: 2017/09/22 13:28 (external edit)