Raising the precedence of the new operator

From: Date: Mon, 05 Apr 2021 16:40:49 +0000
Subject: Raising the precedence of the new operator
Groups: php.internals 
Request: Send a blank email to internals+get-113953@lists.php.net to get a copy of this message
I was wondering... PHP is the only language I know of where you have to
write (new Foo())->bar() instead of
new Foo()->bar(). This is particularly apparent with the builder pattern:

$developer->drink((new Coffee())->withCream()->withSugar());
$logger->log((new LogMessage())->withMessage('Coffee was drunk'));

Since new Foo()->bar() cannot (and probably should not) be used to
"dynamically instantiate a new thing of the class
name returned by function Foo()", it seems like it would be no problem to
change the precedence rules so that
new Foo()->bar() means "instantiate a new Foo and call bar() on it".

It's currently a syntax error, so allowing it would be automatically
compatible.

Has this ever been discussed before?


Thread (14 messages)

« previous php.internals (#113953) next »