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!
85%
15%
1

This is good, many times I've written code like that, and it just did not worked.

Share:
williandamasceno avatar
williandamasceno
voted yes
1

I think static constructors creates a nicer API, but if it is between this and nothing I'm leaning yes.

I would rather write MyClass::new()->doSomething() than new MyClass->doSomething() in most cases. Therefore I would have liked all PHP classes to have a static constructor as well. This constructor could of course be overwritten if you want so it would not break any existing code. These two options are also not mutually exclusive, so I would be happy if they could co-exist.

Share:
pelmered avatar
pelmered
voted yes
1

Waiting for this for so long! Can finally ditch al the MyClass::create() static constructors.

Share:
jorisvaesen avatar
jorisvaesen
voted yes
1

I always thought it was weird that the new <class> operator apparently had a lower precedence than a method call.

To me parenthesis always just means "we need group this set of operations to ensure they're interpreted in the correct order". Given that all else being equal, precedence goes from left to right, the class constructor requiring its precedence to be overridden implies it is a lower precedence.

This RFC fixes that; it gives the constructor equal precedence to other method calls and if you simply read from left to right, it makes sense the new <class> will be executed first, and then the method call.

Anyone arguing that the old syntax is more readable is insane. Who in their right mind would willfully prefer adding more repeated closing parentheses?

$foo = (new MyClass(new OtherClass('foo')))->doSomething();

vs

$foo = new MyClass(new OtherClass('foo'))->doSomething();

It may seem the old syntax is more readable, because you're used to it. However, all things being equal, without any bias, adding more parenthesis will objectively increase cognitive load.

At any rate, if you prefer more parentheses, this feature won't stop you from adding as much parentheses as you like.

Share:
yogarine avatar
yogarine
voted yes
1

Much cleaner syntax and much easier to write. Adding parenthesis has always felt a bit clunky and unnecessary. This would remove the need for a static method for the sake of method chaining.

Share:
RVxLab avatar
RVxLab
voted yes
1

cleaner

Share:
ahmetsabri avatar
ahmetsabri
voted yes
1

Just as any other modern language, a good change. Seems as it's still optional to have parentheses, I don't see a problem

Share:
luis-engelniederhammer avatar
luis-engelniederha...
voted yes
1

I think, it is not correct way of a clean code attempt, removing parentheses will reduce code readability and clarity, especially for those new to PHP or maintaining others' code, it also will introduce inconsistencies in code bases where some use parentheses and others do not. It might increase the risk of subtle bugs, particularly in complex expressions or when refactoring code.

(new Highlighter())->withGutter(); is much more clear to separate visually

Share:
erce avatar
erce
voted no
1

It makes the code more clean :-)

Share:
martijnambagtsheer avatar
martijnambagtsheer
voted yes
1

Better readability; less code is better code

Share:
ricardoboss avatar
ricardoboss
voted yes
1

Agree

Share:
clouetjm avatar
clouetjm
voted yes
1

better readability

Share:
mehranhadidi avatar
mehranhadidi
voted yes
1

Less characters without loss of readability is always good.

Share:
alex-wells avatar
alex-wells
voted yes
1

Looks cool. Hope it does not have weird problems and will not affect performance.

Share:
raszekster avatar
raszekster
voted yes
1

Readable and clean

Share:
hasanablak avatar
hasanablak
voted yes

Check out another RFCs

The Pipe Operator

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

93
272 yes
131 no
Asymmetric Visibility v2

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

18
16 yes
39 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!