TraverseList::using()
Since v1.8.0
Description
TraverseList::using() - iterate over a PHP list (array, Traversable object, or object with public properties)
TraverseList::using() is a convenience wrapper that accepts any recognised PHP list type. Underneath, it calls TraverseArray::using() or TraverseObject::using() as appropriate.
If you're writing a reusable PHP library or component, use TraverseList::using() so that your component is as reusable as possible.
use GanbaroDigital\MissingBits\ListTraversals\TraverseList;
void TraverseList::using(mixed $list, string $listName, callable $callable);
Parameters
TraverseList::using() takes three parameters:
mixed $list- the list to iterate overstring $listName- what you call$listin the calling codecallable $callable- the function to call with each member of$list
$list can be any of the following:
| Type | Behaviour |
|---|---|
| PHP array | passed to TraverseArray::using() |
object that implements Traversable |
passed to TraverseArray::using() |
| any other PHP object | passed to TraverseObject::using() |
| anything else | InvalidArgumentException thrown |
$callable is any PHP callable. It should take three parameters:
$callable = function($value, $key, $name) {
// .. do something here
}
mixed $value- a single entry from$listmixed $key- the array index or object attribute of$valuestring $name- the human-readable name of$value, suitable for including in exception messages and logs
Return Value
TraverseList::using() does not return a value.
Throws
TraverseList::using() throws an InvalidArgumentException if $list is not a supported list type.
Works With
TraverseList::using() and traverse_list() are supported on these versions of PHP:
| PHP Version | Works? |
|---|---|
| 5.5 | Yes |
| 5.6 | Yes |
| 7.0 | Yes |
| HHVM | Yes |