Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112394 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 18474 invoked from network); 3 Dec 2020 10:23:33 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Dec 2020 10:23:33 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C0FF51804C9 for ; Thu, 3 Dec 2020 01:51:25 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,HTML_MESSAGE, SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-mahalux.mvorisek.com (mail-mahalux.mvorisek.com [77.93.195.127]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 3 Dec 2020 01:51:23 -0800 (PST) Received: from 09aa49941cc5 (10.228.0.147) by mail-mahalux.mvorisek.com (10.228.0.4) with Microsoft SMTP Server (TLS); Thu, 3 Dec 2020 10:51:19 +0100 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="=_6026e522172b37bf73d9276bd8c1e8e7" Date: Thu, 03 Dec 2020 10:51:18 +0100 To: =?UTF-8?Q?Micha=C5=82_Marcin_Brzuchalski?= Cc: PHP internals In-Reply-To: References: <91312fb6f300ab9fed5025fc009832c7dd0a15964dc828936e3dc2d0aabb2a1a@mahalux.com> Message-ID: <2166180e533b1a5aa2f95f29d44cef17435d6b951621b83ed045174cf2bbd9e1@mahalux.com> X-Mailer: SAP NetWeaver 7.03 Subject: Re: [PHP-DEV] Allow new Cl()->method() From: vorismi3@fel.cvut.cz (=?UTF-8?Q?Michael_Vo=C5=99=C3=AD=C5=A1ek_-_=C4=8CVUT_FEL?=) --=_6026e522172b37bf73d9276bd8c1e8e7 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset=UTF-8; format=flowed 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 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 --=_6026e522172b37bf73d9276bd8c1e8e7--