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()();
This is good, many times I've written code like that, and it just did not worked.
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.
Waiting for this for so long! Can finally ditch al the MyClass::create()
static constructors.
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.
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.
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 :-)
Better readability; less code is better code
Agree
better readability
Less characters without loss of readability is always good.
Looks cool. Hope it does not have weird problems and will not affect performance.
Readable and clean
The "pipe operator" |>
allows you to chain multiple function calls in a more convenient way.
A new syntax for declaring the “set” operation visibility of an object property
Interface Default Methods improves backwards compatibility when changing interfaces, but also add a way to have multi-inheritance in PHP.