Req #70549 [Asn]: Allow `new Foo()->bar()` without parens around `(new Foo)`
| From: | nikic@php.net | Date: | Fri, 25 Sep 2015 07:17:44 +0000 |
| Subject: | Req #70549 [Asn]: Allow `new Foo()->bar()` without parens around `(new Foo)` | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196225@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: Assigned
Type: Feature/Change Request
Package: Scripting Engine problem
PHP Version: 7.0.0RC3
-Assigned To: nikic
+Assigned To:
Block user comment: N
Private report: N
New Comment:
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'].
Previous Comments:
------------------------------------------------------------------------
[2015-09-25 07:07:25] kalle@php.net
(assigning to Nikita for his PoV on this)
------------------------------------------------------------------------
[2015-09-25 03:59:09] yohgaki@php.net
Related RFC
https://wiki.php.net/rfc/uniform_variable_syntax
PHP 7 evaluates expression left to right, but individually in this case. i.e. "new" is
non-associative.
http://php.net/manual/en/language.operators.precedence.php
Other language's operator precedence.
JavaScript: http://www.scriptingmaster.com/javascript/operator-precedence.asp
C++: http://en.cppreference.com/w/cpp/language/operator_precedence
C: http://en.cppreference.com/w/c/language/operator_precedence
Java: http://introcs.cs.princeton.edu/java/11precedence/
Python3: https://docs.python.org/3/reference/expressions.html
Ruby: http://www.techotopia.com/index.php/Ruby_Operator_Precedence
------------------------------------------------------------------------
[2015-09-22 15:38:35] enoquejoseneas at gmail dot com
Good idea. PHP need this feature, already available on Java and Javascript
------------------------------------------------------------------------
[2015-09-22 07:04:11] vovan-ve at yandex dot ru
Description:
------------
PHP 5.4 allowed us to chain member assess to new Foo, but there must be parens:
(new Foo)->bar();
(new Foo(42, 37))->bar();
something((new Foo(42, 37))->lorem()->ipsum()->dolor());
Why do we need that redundant parenses? Compare what ECMAScript allows - you know:
new Foo().method();
new Foo().field;
new some.where.Foo().method();
new Foos[i]().method();
new (anything)().method();
PHP 7 should to remove redundant parens.
Test script:
---------------
class Foo {
protected $id;
public function __construct($id) {
echo "new ", get_called_class(), "($id)\n";
$this->id = $id;
}
public function __toString() {
return get_class($this) . "{" . $this->id . "}";
}
public function bar($x) {
echo $this, "->", __FUNCTION__, "($x)\n";
return $this;
}
public function val($x) {
echo $this, "->", __FUNCTION__, "($x) => $x\n";
return $x;
}
public function lorem(self $foo, $x) {
echo $this, "->", __FUNCTION__, "($foo, $x)\n";
$foo->bar($x);
return $this;
}
}
new Foo(10)->bar(20)->bar(30)->bar(new Foo(40)->val(50));
new Foo(60)->lorem(new Foo(70)->bar(80), new Foo(90)->val(100));
// same as
// (new Foo(10))->bar(20)->bar(30)->bar((new Foo(40))->val(50));
// (new Foo(60))->lorem((new Foo(70))->bar(80), (new Foo(90))->val(100));
Expected result:
----------------
new Foo(10)
Foo{10}->bar(20)
Foo{10}->bar(30)
new Foo(40)
Foo{40}->val(50) => 50
Foo{10}->bar(50)
new Foo(60)
new Foo(70)
Foo{70}->bar(80)
new Foo(90)
Foo{90}->val(100) => 100
Foo{60}->lorem(Foo{70}, 100)
Foo{70}->bar(100)
Actual result:
--------------
Parse error: syntax error, unexpected '->' (T_OBJECT_OPERATOR)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70549&edit=1