new MyClass()->method() without parentheses

This RFC allows to omit parentheses around the new expression when constructor arguments' parentheses are present.

$highlighter = (new Highlighter())->withGutter();
$highlighter = new Highlighter()->withGutter();

It also works with accessing properties, constants, etc.

new MyClass()::CONSTANT;
new MyClass()::$staticProperty;
new MyClass()::staticMethod();
new MyClass()->property;
new MyClass()->method();
new MyClass()();
Click the bar to cast your vote!
78%
22%
10

Often, I add static methods to some classes in order to improve readability when I instantiate a new class; the ability of chaining methods directly to the constructor, without the unnecessary parenthesis boilerplate, would remove the need for that.

Share:
reddalo avatar
reddalo
voted yes
7

Every time I find myself typing new MyClass() and than returning back and adding those parentheses. It would be good to reduce this friction and make PHP coding flow smoother.

Share:
pronskiy avatar
pronskiy
voted yes
3

I don't think removing parenthese is a good idea

Share:
iyogesharma avatar
iyogesharma
voted no
3

Cleaner code

Share:
tomsix avatar
tomsix
voted yes
3

this allows for cleaner code. less verbose.

Share:
davidcarrdev avatar
davidcarrdev
voted yes
2

makes for cleaner code by removing those useless parentheses

Share:
andreas avatar
andreas
voted yes
2

Maybe that method should be static...

Share:
lotharthesavior avatar
lotharthesavior
voted no
2

As a general user with no actual knowledge of the internals of PHP I cannot foresee any challenges. However, I feel this could be a useful addition to the language itself. There are a lot of times where I've seen code (new MyClass)->method() which always felt a bit unnecessary.

One thing that feels kinda weird now is the static method part. My question with that one is, will the old behaviour still work? Example: MyClass::staticMethod()? I am interested to learn what the community thinks of the change and how they view it.

Share:
wotta avatar
wotta
voted yes
1

JavaScript has this, and I've never found the lack of parentheses confusing there. If anything, I think the currently-required wrapper parentheses in PHP add cognitive load when parsing out a statement.

Share:
zeb avatar
zeb
voted yes
1

It is very annoying to wrap new class into parentheses.

Share:
theese avatar
theese
voted yes
1

Yeah finally. its necessary

Share:
rezamasoudi avatar
rezamasoudi
voted yes
1

This syntax, will make the code more readable, clean.

Share:
hamzatopo avatar
hamzatopo
voted yes
1

This is very clean

Share:
williamug avatar
williamug
voted yes
1

I love it, it's cleaner

Share:
cesarmtorres avatar
cesarmtorres
voted yes
1

It is really confusing to read since having the space breaks the way PHP handles spaces in all other places. It is incoherent.

Share:
noah avatar
noah
voted no

Check out another RFCs

Short Closures 2.0

This RFC proposes a way to have multi-line short closures — closures that don't need explicit use statements, but can still have multiple lines and a return statement.

101
373 yes
66 no
The Pipe Operator

The "pipe operator" |> allows you to chain multiple function calls in a more convenient way.

90
262 yes
122 no
Property Hooks

A new way of interacting with properties

64
122 yes
78 no
RSS Feed Contribute Watch on YouTube Our License
© 2024 RFC Vote. This project is open source. Contribute and collaborate with us!