[php-src] Issue #9556: "iterable" alias "array|Traversable" breaks PHP 8.1 code
| From: | cziegenberg | Date: | Fri, 16 Sep 2022 12:23:17 +0000 |
| Subject: | [php-src] Issue #9556: "iterable" alias "array|Traversable" breaks PHP 8.1 code | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242428@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9556
Author: 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
<?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_