Re: [RFC] [Discussion] new MyClass()->method() without parentheses
| From: | Valentin Udaltsov | Date: | Tue, 09 Apr 2024 19:40:24 +0000 |
| Subject: | Re: [RFC] [Discussion] new MyClass()->method() without parentheses | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-123070@lists.php.net to get a copy of this message | ||
>
> On Mon, Apr 8, 2024, at 6:08 AM, 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
>
> I always thought there was some technical parser reason why this wasn't
> possible. Maybe that was true in 5.x but isn't anymore?
>
> I cannot speak to the implementation, but I'm all for the change itself.
>
> --Larry Garfield
>
Hi, Larry! The grammar is compiled with no warnings, so it is definitely
possible now. I also added a lot of tests that guarantee that existing
behaviour is preserved and new syntax works as expected.
Marco, Bilge, Levi, Larry, thank you for your positive feedback on the RFC.