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!
86%
14%
20

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
16

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
7

this allows for cleaner code. less verbose.

Share:
davidcarrdev avatar
davidcarrdev
voted yes
4

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
4

I don't think removing parenthese is a good idea

Share:
iyogesharma avatar
iyogesharma
voted no
4

This is one of the things that I come across so often, numerous times within every project. It makes sense for PHP to get rid of boilerplate wherever it can. If the compiler can figure out the details, then I prefer not having to micro-manage every bracket.

Share:
Contributor brent avatar
brent
voted yes
3

Maybe that method should be static...

Share:
lotharthesavior avatar
lotharthesavior
voted no
3

Cleaner code

Share:
tomsix avatar
tomsix
voted yes
2

Agree

Share:
clouetjm avatar
clouetjm
voted yes
2

It just makes sense and also optional, so whoever doesn't like it can go back to using parentheses.

Share:
medabkari avatar
medabkari
voted yes
2

makes for cleaner code by removing those useless parentheses

Share:
andreas avatar
andreas
voted yes
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

It seems like there’s a strong consensus that removing the need for parentheses when instantiating a new class in PHP would make the code cleaner and more intuitive. Many of us find it to be unnecessary noise that adds friction to the coding flow. However, there are some concerns about maintaining existing functionality, like static methods. It would be great to hear more about how this change would impact the current syntax and whether the old behavior will still work.

Share:
shtefcs avatar
shtefcs
voted yes
1

Whenever I had to write (new Class())->method(), it always felt like I'm doing something wrong. Then I found out that it's not that uncommon. So yes, all the way.

Share:
lucilaos avatar
lucilaos
voted yes
1

It's never made sense to me that new doesn't automatically begin the construction of the object, instead we've had to encapsulate it into an order of operations. It's unnecessary noise in the code.

Share:
thookerov avatar
thookerov
voted yes

Check out another RFCs

Asymmetric Visibility v2

A new syntax for declaring the “set” operation visibility of an object property

21
21 yes
38 no
The Pipe Operator

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

93
272 yes
131 no
Interface Default Methods

Interface Default Methods improves backwards compatibility when changing interfaces, but also add a way to have multi-inheritance in PHP.

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