[RFC] [Discussion] new MyClass()->method() without parentheses

php.internals

Удальцов Валентин

2 years ago
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

Deleu

2 years ago
On Mon, Apr 8, 2024 at 3:11 AM Valentin Udaltsov < udaltsov.valentin@gmail.com> 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 >
Yes, please!
-- Marco Deleu

Levi Morrison

2 years ago
I haven't verified the implementation, but as long as there really aren't backwards compatibility issues for correct code, then this would be a nice quality of life improvement.

Bilge

2 years ago
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:

Larry Garfield

2 years ago
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

Удальцов Валентин

2 years ago
> > 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.

Удальцов Валентин

2 years ago
вт, 9 апр. 2024 г. в 19:41, Larry Garfield <larry@garfieldtech.com>:
> 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 >
Does anyone have additional feedback? I'd like to start voting on Thursday, April 25th. I've also added a section on other syntax ideas that have been expressed on Twitter and in the PR: https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas
-- Valentin

Robert Landers

2 years ago
On Tue, Apr 23, 2024 at 11:10 AM Valentin Udaltsov <udaltsov.valentin@gmail.com> wrote:
> > вт, 9 апр. 2024 г. в 19:41, Larry Garfield <larry@garfieldtech.com>: >> >> 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 > > > Does anyone have additional feedback? I'd like to start voting on Thursday, April 25th. > I've also added a section on other syntax ideas that have been expressed on Twitter and in the PR: https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas > -- > Valentin
I suspect this will break (badly written) reflection in interesting ways: https://3v4l.org/mcSNH This basically breaks dereferencing order of operations and makes it inconsistent. Robert Landers Software Engineer Utrecht NL

Lynn

2 years ago
On Tue, Apr 23, 2024 at 1:20 PM Robert Landers <landers.robert@gmail.com> wrote:
> On Tue, Apr 23, 2024 at 11:10 AM Valentin Udaltsov > <udaltsov.valentin@gmail.com> wrote: > > > > вт, 9 апр. 2024 г. в 19:41, Larry Garfield <larry@garfieldtech.com>: > >> > >> 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 > > > > > > Does anyone have additional feedback? I'd like to start voting on > Thursday, April 25th. > > I've also added a section on other syntax ideas that have been expressed > on Twitter and in the PR: > https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas > > -- > > Valentin > > I suspect this will break (badly written) reflection in interesting ways: > > https://3v4l.org/mcSNH > > This basically breaks dereferencing order of operations and makes it > inconsistent. > > Robert Landers > Software Engineer > Utrecht NL >
I think these scenarios are all covered in the RFC under "This RFC still does not allow to omit parentheses around the new expression without constructor arguments' parentheses, because in some cases this leads to an ambiguity"

Alexandru Pătrănescu

2 years ago
On Tue, Apr 23, 2024 at 3:31 PM Robert Landers <landers.robert@gmail.com> wrote:
> On Tue, Apr 23, 2024 at 11:10 AM Valentin Udaltsov > <udaltsov.valentin@gmail.com> wrote: > > > > > > Does anyone have additional feedback? I'd like to start voting on > Thursday, April 25th. > > I've also added a section on other syntax ideas that have been expressed > on Twitter and in the PR: > https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas > > -- > > Valentin > > I suspect this will break (badly written) reflection in interesting ways: > > https://3v4l.org/mcSNH > > This basically breaks dereferencing order of operations and makes it > inconsistent. >
Quote from RFC: "RFC still does not allow to omit parentheses around the new expression *without* constructor arguments' parentheses, because in some cases this leads to an ambiguity" And actually it mentions a list in the RFC: // Instantiate and then access the instance or instantiate the result of the expression?new MyClass::CONSTANT <http://www.php.net/constant>;new MyClass::$staticProperty;new $myClass::CONSTANT <http://www.php.net/constant>;new $myClass::$staticProperty;new $myClass->property;new $myClass->method(); But actually from all of those, right now, only "new MyClass::CONSTANT <http://www.php.net/constant>;" and "*new* $myClass::CONSTANT <http://www.php.net/constant>;" are not working, while the other 4 are working fine. So yeah, this needs clarification if they continue to work as they work right now: https://3v4l.org/PmCfR Thanks, Alex

Удальцов Валентин

2 years ago
@Lynn, @Alex, thank you for your comments. I have improved the "without constructor arguments' parentheses" part of the introduction section and started the voting. вт, 23 апр. 2024 г. в 16:55, Alexandru Pătrănescu <drealecs@gmail.com>:
> > On Tue, Apr 23, 2024 at 3:31 PM Robert Landers <landers.robert@gmail.com> > wrote: > >> On Tue, Apr 23, 2024 at 11:10 AM Valentin Udaltsov >> <udaltsov.valentin@gmail.com> wrote: >> > >> > >> > Does anyone have additional feedback? I'd like to start voting on >> Thursday, April 25th. >> > I've also added a section on other syntax ideas that have been >> expressed on Twitter and in the PR: >> https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas >> > -- >> > Valentin >> >> I suspect this will break (badly written) reflection in interesting ways: >> >> https://3v4l.org/mcSNH >> >> This basically breaks dereferencing order of operations and makes it >> inconsistent. >> > > Quote from RFC: > "RFC still does not allow to omit parentheses around the new expression > *without* constructor arguments' parentheses, because in some cases this > leads to an ambiguity" > > And actually it mentions a list in the RFC: > > // Instantiate and then access the instance or instantiate the result of the expression?new MyClass::CONSTANT <http://www.php.net/constant>;new MyClass::$staticProperty;new $myClass::CONSTANT <http://www.php.net/constant>;new $myClass::$staticProperty;new $myClass->property;new $myClass->method(); > > But actually from all of those, right now, only "new MyClass::CONSTANT > <http://www.php.net/constant>;" and "*new* $myClass::CONSTANT > <http://www.php.net/constant>;" are not working, while the other 4 are > working fine. > > So yeah, this needs clarification if they continue to work as they work > right now: > https://3v4l.org/PmCfR > > Thanks, > Alex >
-- С уважением, Валентин

Larry Garfield

2 years ago
On Thu, May 9, 2024, at 3:26 PM, Valentin Udaltsov wrote:
> @Lynn, @Alex, thank you for your comments. I have improved the "without > constructor arguments' parentheses" part of the introduction section > and started the voting.
I believe standard procedure expects you to post a new message to the list, entitled `[RFC] {Vote] some title`, to announce the start of the vote and the date it will end. --Larry Garfield

Удальцов Валентин

2 years ago
Yes, I've done that, see https://externals.io/message/123293. чт, 9 мая 2024 г. в 20:11, Larry Garfield <larry@garfieldtech.com>:
> On Thu, May 9, 2024, at 3:26 PM, Valentin Udaltsov wrote: > > @Lynn, @Alex, thank you for your comments. I have improved the "without > > constructor arguments' parentheses" part of the introduction section > > and started the voting. > > I believe standard procedure expects you to post a new message to the > list, entitled `[RFC] {Vote] some title`, to announce the start of the vote > and the date it will end. > > --Larry Garfield >
-- С уважением, Валентин

Larry Garfield

2 years ago
On Thu, May 9, 2024, at 5:12 PM, Valentin Udaltsov wrote:
> Yes, I've done that, see https://externals.io/message/123293.
D'Oh. At almost the exact same time I replied. :-) Thanks. (Also, please do not top post.) --Larry Garfield