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

- Thread: https://externals.io/message/123031
- Original archive: https://news-web.php.net/php.internals/123031
- Started by Valentin Udaltsov on 2024-04-08T06:08:11+00:00
- Messages: 14
- RFC: new MyClass()->method() without parentheses (Implemented (PHP 8.4)) — https://externals.io/rfc/new_without_parentheses.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #123031 — Valentin Udaltsov — 2024-04-08T06:08:11+00:00

URL: https://externals.io/message/123031#123031

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

---

## #123040 — Deleu — 2024-04-08T11:32:02+00:00

URL: https://externals.io/message/123031#123040 · In reply to: #123031

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

---

## #123047 — Bilge — 2024-04-08T21:11:17+00:00

URL: https://externals.io/message/123031#123047 · In reply to: #123031

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


>
> 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

---

## #123060 — Larry Garfield — 2024-04-09T15:23:59+00:00

URL: https://externals.io/message/123031#123060 · In reply to: #123031

> 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

---

## #123063 — Levi Morrison — 2024-04-09T16:22:59+00:00

URL: https://externals.io/message/123031#123063 · In reply to: #123040

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.

---

## #123070 — Valentin Udaltsov — 2024-04-09T19:40:24+00:00

URL: https://externals.io/message/123031#123070 · In reply to: #123060

>
> 
> > […]
> 'staticMethod'; }
> > […]
> https://wiki.php.net/rfc/new_without_parentheses
> > […]
>
> 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.

---

## #123174 — Valentin Udaltsov — 2024-04-23T09:07:02+00:00

URL: https://externals.io/message/123031#123174 · In reply to: #123060

вт, 9 апр. 2024 г. в 19:41, Larry Garfield <larry@garfieldtech.com>:

> 
> > […]
> 'staticMethod'; }
> > […]
> https://wiki.php.net/rfc/new_without_parentheses
> > […]
>
> 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

---

## #123179 — Robert Landers — 2024-04-23T11:18:14+00:00

URL: https://externals.io/message/123031#123179 · In reply to: #123174

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>:
> > […]
>
>
> 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

---

## #123180 — Lynn — 2024-04-23T12:51:26+00:00

URL: https://externals.io/message/123031#123180 · In reply to: #123179

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:
> > […]
> expression
> > […]
> 'staticMethod'; }
> > […]
> https://wiki.php.net/rfc/new_without_parentheses
> > […]
> possible.  Maybe that was true in 5.x but isn't anymore?
> > […]
> Thursday, April 25th.
> > […]
> on Twitter and in the PR:
> https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas
> > […]
>
> 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"

---

## #123185 — drealecs@gmail.com — 2024-04-23T13:54:52+00:00

URL: https://externals.io/message/123031#123185 · In reply to: #123179

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:
> > […]
> Thursday, April 25th.
> > […]
> on Twitter and in the PR:
> https://wiki.php.net/rfc/new_without_parentheses#other_syntax_ideas
> > […]
>
> 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

---

## #123292 — Valentin Udaltsov — 2024-05-09T15:26:16+00:00

URL: https://externals.io/message/123031#123292 · In reply to: #123185

@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:
>
> > […]
>
> 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
>


-- 
С уважением, Валентин

---

## #123294 — Larry Garfield — 2024-05-09T16:06:06+00:00

URL: https://externals.io/message/123031#123294 · In reply to: #123292

> @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

---

## #123295 — Valentin Udaltsov — 2024-05-09T17:12:52+00:00

URL: https://externals.io/message/123031#123295 · In reply to: #123294

Yes, I've done that, see https://externals.io/message/123293.

чт, 9 мая 2024 г. в 20:11, Larry Garfield <larry@garfieldtech.com>:

> 
> > […]
>
> 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
>


-- 
С уважением, Валентин

---

## #123296 — Larry Garfield — 2024-05-09T17:33:07+00:00

URL: https://externals.io/message/123031#123296 · In reply to: #123295

> 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
