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()();
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.
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.
this allows for cleaner code. less verbose.
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.
I don't think removing parenthese is a good idea
Cleaner code
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.
It just makes sense and also optional, so whoever doesn't like it can go back to using parentheses.
makes for cleaner code by removing those useless parentheses
Maybe that method should be static...
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.
cleaner
Just as any other modern language, a good change. Seems as it's still optional to have parentheses, I don't see a problem
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
It makes the code more clean :-)
The "pipe operator" |>
allows you to chain multiple function calls in a more convenient way.
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.