[php-src] Issue #14547: Unexpected behaviour with the nullsafe operator in combination with an invocable class.
| From: | heiglandreas | Date: | Wed, 12 Jun 2024 15:25:22 +0000 |
| Subject: | [php-src] Issue #14547: Unexpected behaviour with the nullsafe operator in combination with an invocable class. | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247080@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/14547
Author: heiglandreas
### Description
The following code:
```php
<?php
class Foo {
public function __invoke(): string {
return 'foo invoked';
}
}
$f = new Foo();
var_Dump($f());
```
results in this expected output:
```
string(11) "foo invoked"
```
```php
$g = null;
$g();
```
results in this expected output
```
Fatal error: Uncaught Error: Value of type null is not callable
```
```php
$g = null;
$g?->__invoke();
```
results in this expected output:
```
string(11) "foo invoked"
```
As the call to
->__invoke() can be shortened to () I would have
expected to be able to use the nullsafe operator here as well and write
```php
$g = null;
$g?();
```
But this results in in this output:
```
Parse error: syntax error, unexpected token ")"
```
I would have expected
```
string(11) "foo invoked"
```
Is this on purpose? I could not find any mention of invoke in the
[RFC](https://wiki.php.net/rfc/nullsafe_operator). So was this overseen?
See also https://3v4l.org/n09i2
### PHP Version
All currently supported PHP-Versions
### Operating System
_No response_