iterable

  • Iterable is a pseudo-type introduced in PHP 7.1.
  • It accepts any array or object implementing the Traversable interface.
  • Can be used as a parameter type to indicate that a function requires a set of values, but does not care about the form of the value set since it will be used with foreach.
  • Can also be used as a return type to indicate a function will return an iterable value.
  • Functions declaring iterable as a return type may also be generators.

Iterable Type Variance

<?php

interface Example {
    public function method(array $array): iterable;
}

class ExampleImplementation implements Example {
    public function method(iterable $iterable): array {
        // Parameter broadened and return type narrowed.
    }
}

?>

Classes extending/implementing may broaden methods using array or Traversable as parameter types to iterable or narrow return types from iterable to array or Traversable.

iterable — Structure map

Clickable & Draggable!

iterable — Related pages: