Re: Allow new Cl()->method()

From: Date: Thu, 03 Dec 2020 09:51:18 +0000
Subject: Re: Allow new Cl()->method()
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-112394@lists.php.net to get a copy of this message
In my opinion: - the operator table docs should be updated to contain all operators ( see comment https://www.php.net/manual/en/language.operators.precedence.php#121509 ) - "new" should have the highest absolute priority, ie. new Cl()->method(); should be always interpreted as (new Cl())->method();, new (Cl()->method()); is very unusual and parenthesis can be always used to enforce the intended precedence. With kind regards / Mit freundlichen Grüßen / S přátelským pozdravem, Michael Voříšek ČVUT FEL On 3 Dec 2020 07:45, Michał Marcin Brzuchalski wrote:
Hi Michael, śr., 2 gru 2020 o 21:03 Michael Voříšek - ČVUT FEL <vorismi3@fel.cvut.cz> napisał(a):
Hi devs, currently new Cl() must be wrapped in parentheses if that object is further used to call it's method like (new Cl())->method(). It there any reason why not to allow new Cl()->method() ? Based on https://www.php.net/manual/en/language.operators.precedence.php - the "new" operator has already the highest precedence, thus there should be no conflict.
I think that's because removing the parentheses may confuse what was the original intention of author. class CI { public function method() { echo 'foo'; } } class bar { public function __construct() { echo 'bar'; } } function CI() { return new class { public function method() { return 'bar'; } }; } (new CI())->method(); // foo new (CI()->method()); // bar Both ways are valid code in PHP8 with no error/warning, till PHP7.4 there was a Parse error: syntax error, unexpected '(' in the second object construction. Cheers, Michał Marcin Brzuchalski


« previous php.internals (#112394) next »