# ReflectionType for iterable / PHP 8.2

- Thread: https://externals.io/message/118939
- Original archive: https://news-web.php.net/php.internals/118939
- Started by Benjamin Morel on 2022-11-02T17:46:15+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #118939 — Benjamin Morel — 2022-11-02T17:46:15+00:00

URL: https://externals.io/message/118939#118939

Hi internals,

It just came to my attention that there is a change of behaviour between
PHP 8.1 and 8.2 in the way iterable is decomposed, or not, into
Traversable|array when reflected:

```
function foo(): iterable {}
function bar(): stdClass|iterable {}

echo (new ReflectionFunction('foo'))->getReturnType(), PHP_EOL;
echo (new ReflectionFunction('bar'))->getReturnType(), PHP_EOL;
```

Output on PHP 8.1:

```
iterable
stdClass|iterable
```

Output on PHP 8.2:

```
iterable
stdClass|Traversable|array
```

Is this expected behaviour? Or should I file a bug? I'm particularly
surprised that it behaves this way on PHP 8.2 only in the presence of union
types.

Thank you,
Benjamin

---

## #118942 — Juliette Reinders Folmer — 2022-11-02T18:54:12+00:00

URL: https://externals.io/message/118939#118942 · In reply to: #118939

> Hi internals,
>
> It just came to my attention that there is a change of behaviour between
> PHP 8.1 and 8.2 in the way iterable is decomposed, or not, into
> Traversable|array when reflected:
>
> ```
> function foo(): iterable {}
> function bar(): stdClass|iterable {}
>
> echo (new ReflectionFunction('foo'))->getReturnType(), PHP_EOL;
> echo (new ReflectionFunction('bar'))->getReturnType(), PHP_EOL;
> ```
>
> Output on PHP 8.1:
>
> ```
> iterable
> stdClass|iterable
> ```
>
> Output on PHP 8.2:
>
> ```
> iterable
> stdClass|Traversable|array
> ```
>
> Is this expected behaviour? Or should I file a bug? I'm particularly
> surprised that it behaves this way on PHP 8.2 only in the presence of union
> types.
>
> Thank you,
> Benjamin
>
That's a intentional behaviour change and related to this accepted PHP 
8.2 RFC: https://wiki.php.net/rfc/iterator_xyz_accept_array

Smile,
Juliette
