Le 07/06/2024 à 18:03, Benoît Condaminet a écrit :
Hello,

Following the RFC process, I'm sending this to propose a PHP change. More precisely a new zend language token.

This is somehow linked to the recently accepted RFC called "new MyClass()->method() without parentheses", the goal is to introduce a shorthand for the "new" keyword.

*Motivations :*
The new keyword sometime has a bad DX, for example when chaining Class instantiation like this :
$foobar = new Foo( new Bar(), new Etc());

We can quickly have very long class object construction in some case.

In lot of Framework, to improve DX, static function construct are often used to avoid the new keyword, some create additional object builders, etc.

As a first try, I start updated code to completely make the new keyword optional, like in Dart language for example, but it require very big change, with lot of impact (collision with function).

So here is *my proposal : *

Add a "new" shorthand, using the tilde character : "~"
I made a POC, and it works well, declaring a new language token T_SHORT_NEW that simply reuse ZEND_AST_NEW under the hood.

Hello,

I sincerely do not want to see a new operator for replacing the "new" operator, because then we would have two different syntaxes which would be semantically equivalent.

Creating a new object, in most API design, is not something you do very often, especially when working in a framework that does it for you (dependency injection, etc...).

And, moreover, when I'm searching who what where was created an specific class instance, I regex grep for "new\s+ClassName" and in most case, it works like a charm. If you add an operator for this, it simply will create a huge cognitive dissonance for simply no added value at all.

"New" is working fine, adding a shortcut for the sake of adding a shortcut to something you don't write that much doesn't worth it in my opinion: there are much more disadvantages that benefits.

Best regards,

Pierre

Reply via email to