Hi Valentin,
You don't need me to tell you how popular your PR is, since it's the
single most emoji'd open PR by a wide margin, but this is something that
has bugged me since the parser improvements in 5.4. I even remember
asking Nikita if it was possible to not have to wrap `new Class` in
parens to dereference it, and he said it was, but just didn't do it for
whatever reason.
It's frustrating using a PHP REPL (e.g. Boris, PsySH) and wanting to
write `new Class->foo()` only to have to backtrack and add the parens,
because it's not natural (for me) to think in terms of the parens ahead
of time. Even in the editor, it's the same experience; I often find
myself backtracking to add the parens. Perhaps this seems like a small
thing to some people, but for me this could be the sole reason to day-1
upgrade to 8.4.
But I have no voting privileges so I can only offer you my thanks.
Thanks for this!
Cheers,
Bilge
On 08/04/2024 07:08, Valentin Udaltsov wrote:
Hello internals,
I would like to propose a syntax change for PHP 8.4 that allows to
immediately access instantiated objects without wrapping the
expression into parentheses.
This was requested 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
Here's what you will be able to write after this change:
```php
class MyClass
{
const CONSTANT = 'constant';
public static $staticProperty = 'staticProperty';
public static function staticMethod(): string { return 'staticMethod'; }
public $property = 'property';
public function method(): string { return 'method'; }
public function __invoke(): string { return '__invoke'; }
}
var_dump(
new MyClass()::CONSTANT,// string(8)"constant"
new MyClass()::$staticProperty, // string(14) "staticProperty"
new MyClass()::staticMethod(),// string(12) "staticMethod"
new MyClass()->property,// string(8)"property"
new MyClass()->method(),// string(6)"method"
new MyClass()(),// string(8)"__invoke"
);
```
For more details see the RFC:
https://wiki.php.net/rfc/new_without_parentheses
Implementation: https://github.com/php/php-src/pull/13029
--
Best regards, Valentin