Req #70549 [Opn]: Allow `new Foo()->bar()` without parens around `(new Foo)`

From: Date: Fri, 25 Sep 2015 13:57:38 +0000
Subject: Req #70549 [Opn]: Allow `new Foo()->bar()` without parens around `(new Foo)`
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-196241@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=70549&edit=1 ID: 70549 Updated by: nikic@php.net Reported by: vovan-ve at yandex dot ru Summary: Allow new Foo()->bar() without parens around `(new Foo)` Status: Open Type: Feature/Change Request Package: Scripting Engine problem PHP Version: 7.0.0RC3 Block user comment: N Private report: N New Comment: In that case it should be very easy to support this. Something along these lines: https://gist.github.com/nikic/47ee4f5e67bc90866099 This allows new Foo()->bar(), new Foo()->bar, new Foo()['bar']. Adding it to callable vars as well would allow new Foo()(). So from the technical side this is no problem. I don't know if we should actually allow it, as the syntax looks somewhat ambiguous. I'm neutral regarding that. If we want this change, somebody should RFC it. Previous Comments: ------------------------------------------------------------------------ [2015-09-25 08:20:01] vovan-ve at yandex dot ru nikic, Yes, right. new Foo->bar() start with Foo->... and it is "unexpected ->". ------------------------------------------------------------------------ [2015-09-25 08:13:02] nikic@php.net Ah, I think I get what you mean now. You only want to allow omission of parentheses if new is used with an argument list, correct? I.e. new Foo->bar() will be forbidden, but new Foo()->bar() will be allowed and interpreted as (new Foo())->bar()? ------------------------------------------------------------------------ [2015-09-25 07:24:27] vovan-ve at yandex dot ru > You are proposing to interpret this as $obj = (new > $foo)->bar() instead No, it's not. new $foo->bar()->method() is exactly new ($foo->bar)()->method() like new foo.bar().method() is ECMAScript. ------------------------------------------------------------------------ [2015-09-25 07:17:42] nikic@php.net We currently allow code like this: $obj = new $foo->bar(); // Meaning: $class = $foo->bar; $obj = new $class(); You are proposing to interpret this as $obj = (new $foo)->bar() instead, which would break backwards compatibility. This could have been changed as part of the UVS RFC, but I felt like this change would be too intrusive and the consequences rather weird. I expect something like new $foo['bar'] to behave as new {$foo['bar']} not (new $foo)['bar']. ------------------------------------------------------------------------ [2015-09-25 07:07:25] kalle@php.net (assigning to Nikita for his PoV on this) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=70549 -- Edit this bug report at https://bugs.php.net/bug.php?id=70549&edit=1

« previous php.bugs (#196241) next »