Skip to content

"iterable" alias "array|Traversable" breaks PHP 8.1 code #9556

Closed
@cziegenberg

Description

@cziegenberg

Description

I just recognized a problem in a project when testing it with PHP 8.2RC2 (API Platform 3.0).

There is an interface that contains a method with the return type definition "object|iterable|null". In PHP 8.2 "iterable" becomes "array|Traversable" and so the return type definition results in "object|array|Traversable|null", which now contains the redundant "Traversable" type (although "object" is already contained) and leads to a fatal error.

Eample...

The following code:

<?php
class Foo
{
    public function bar(): object|iterable|null
    {
        return null;
    }
}

echo 'baz';

Resulted in this output:

Fatal error: Type Traversable|object|array|null contains both object and a class type, which is redundant in [...

But I expected this output instead:

baz

As a workaround one could use "object|array" instead of "object|iterable", but best would be if the change in PHP 8.2 handles this special case, to avoid this break... Otherwise this should be listed in "Backward Incompatible Changes" instead of "Other Changes".

PHP Version

PHP 8.2.0RC2

Operating System

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions