Take the State of PHP 2026 Survey →

Iterable View as Markdown

Implemented (PHP 7.1)
Authors
Aaron Piotrowski aaron@trowski.com
Date
2016-06-10
Version
0.1.1
Source
wiki.php.net/rfc/iterable

Vote

Add iterable type to PHP 7.1 closed
Yes
35
No
2
37 voters
ajf Yes 2016-06-28 17:25
bwoebi Yes 2016-06-25 10:41
cmb Yes 2016-07-01 22:48
colinodell Yes 2016-06-28 18:03
danack Yes 2016-06-24 19:12
daverandom Yes 2016-06-24 18:38
davey Yes 2016-06-24 21:12
francois Yes 2016-06-25 10:31
galvao Yes 2016-06-27 11:32
gooh Yes 2016-06-27 11:08
hywan Yes 2016-06-29 12:39
jwage Yes 2016-06-28 17:56
kalle Yes 2016-06-25 01:13
kguest Yes 2016-06-27 12:55
kinncj Yes 2016-06-29 16:12
krakjoe Yes 2016-06-25 06:16
lbarnaud Yes 2016-07-03 10:24
lcobucci Yes 2016-06-27 11:09
levim Yes 2016-06-27 15:33
marcio No 2016-06-24 21:38
mariano Yes 2016-06-25 16:05
mbeccati Yes 2016-06-26 09:02
mrook Yes 2016-06-28 14:26
nikic Yes 2016-07-02 14:25
ocramius Yes 2016-07-03 03:30
pauloelr Yes 2016-06-27 18:53
pmjones Yes 2016-06-25 18:06
pollita Yes 2016-07-02 23:44
salathe Yes 2016-07-01 08:55
sammyk Yes 2016-06-24 20:11
santiagolizardo Yes 2016-06-29 22:04
stas No 2016-06-24 19:15
thekid Yes 2016-07-03 09:46
trowski Yes 2016-06-24 18:01
weierophinney Yes 2016-06-24 21:03
yunosh Yes 2016-06-25 13:46
zimt Yes 2016-06-28 13:21

Discussions on the mailing list

[RFC] Iterable
by Aaron Piotrowski · 16 messages
[RFC][Vote] Iterable
by Aaron Piotrowski · 1 message
Countable Type Hint
by Craig Duncan · 15 messages

RFC text

Copied from wiki.php.net, last synchronized Oct 13, 2026 11:51 UTC

Introduction

It is common for a function to accept or return either an array or an object implementing Traversable to be used with foreach. However, because array is a primitive type and Traversable is an interface, there currently is no way to use a type declaration on a parameter or return type to indicate that the value is iterable.

Proposal

This RFC proposes a new iterable pseduo-type. This type is analogous to callable, accepting multiple types instead of one single type.

iterable accepts any array or object implementing Traversable. Both of these types are iterable using foreach and can be used with yield from within a generator.

iterable 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 (array, Iterator, Generator, etc.) since it will be used with foreach. If a value is not an array or instance of Traversable, a TypeError will be thrown.

function foo(iterable $iterable) {
    foreach ($iterable as $value) {
        // ...
    }
}

iterable can also be used as a return type to indicate a function will return an iterable value. If the returned value is not an array or instance of Traversable, a TypeError will be thrown.

function bar(): iterable {
    return [1, 2, 3];
}

Parameters declared as iterable may use null or an array as a default value.

function foo(iterable $iterable = []) {
    // ...
}

Functions declaring iterable as a return type may also be generators.

function gen(): iterable {
    yield 1;
    yield 2;
    yield 3;
}

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. This behavior is the same as that proposed for all union types in the Union Types RFC.

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

This proposal also adds a function is_iterable() that returns a boolean: true if a value is iterable and will be accepted by the iterable pseudo-type, false for other values.

var_dump(is_iterable([1, 2, 3])); // bool(true)
var_dump(is_iterable(new ArrayIterator([1, 2, 3]))); // bool(true)
var_dump(is_iterable((function () { yield 1; })())); // bool(true)
var_dump(is_iterable(1)); // bool(false)
var_dump(is_iterable(new stdClass())); // bool(false)

Object Iteration

PHP allows any object to be used with foreach. However, iterable does not accept any object, only those implementing Traversable. Values accepted by iterable should be designed for iteration, not any set of values (such as the public properties of an object or a string). There is already some precedent in the language for discriminating between values accepted by iterable and those that can be used with foreach. yield from only accepts arrays or objects implementing Traversable. iterable then represents the values usable with yield from.

Backward Incompatible Changes

iterable is implemented as reserved classname, therefore a class, interface, or trait named iterable cannot be declared.

Another function named is_iterable() cannot be declared in the root namespace.

Proposed PHP Version(s)

PHP 7.1

Future Scope

Union Types

If the union types RFC is accepted, then iterable will be a shortcut for array | Traversable. This is a common type requirement for functions, so having a shorter name for this union will be handy.

Voting

This proposal requires a 2/3 majority to be accepted.

Voting began on 2016-06-24 at 6:00 pm GMT and ended on 2016-07-02 at 11:59 pm GMT

Vote: Add iterable type to PHP 7.1 — Yes: 35 · No: 2 (closed)

Patches and Tests

PR: #1941