[php-src] PR #13029: new MyClass()->method() without parentheses
| From: | vudaltsov | Date: | Tue, 26 Dec 2023 12:48:25 +0000 |
| Subject: | [php-src] PR #13029: new MyClass()->method() without parentheses | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-24603@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/13029
Author: vudaltsov
This PR allows to immediately access newly created object without parentheses.
```php
class A
{
const CONSTANT = 'constant';
public static $staticProperty = 'staticProperty';
public static function staticMethod() {}
public $property = 'property';
public function method() {}
public function __invoke() {}
}
new A()::CONSTANT;
new A()::$staticProperty;
new A()::staticMethod();
new A()->property;
new A()->method();
new A()();
```
For non-anonymous classes this works only with constructor argument parentheses to avoid ambiguity.
Same applies to anonymous classes:
```php
new class { const CONSTANT = 'constant'; }::CONSTANT;
new class { public static $staticProperty = 'staticProperty'; }::$staticProperty;
new class { public static function staticMethod() {} }::staticMethod();
new class { public $property = 'property'; }->property;
new class { public function method() {} }->method();
new class { public function __invoke() {} }();
```
This was proposed and discussed several times, see:
- https://externals.io/message/66197
- https://bugs.php.net/bug.php?id=70549
- https://externals.io/message/101811
- https://externals.io/message/113953
Ready to make an RFC if this PR makes sense.
TODO
- [x] Regular class tests.
- [ ] Anonymous class tests.