Req #70549 [NEW]: Allow `new Foo()->bar()` without parens around `(new Foo)`
| From: | vovan-ve at yandex dot ru | Date: | Tue, 22 Sep 2015 07:04:12 +0000 |
| Subject: | Req #70549 [NEW]: Allow `new Foo()->bar()` without parens around `(new Foo)` | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-196154@lists.php.net to get a copy of this message | ||
From: vovan-ve at yandex dot ru
Operating system:
PHP version: 7.0.0RC3
Package: Scripting Engine problem
Bug Type: Feature/Change Request
Bug description:Allow
new Foo()->bar() without parens around (new Foo)
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 bug report at https://bugs.php.net/bug.php?id=70549&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70549&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70549&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70549&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70549&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70549&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70549&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70549&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70549&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70549&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70549&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70549&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70549&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70549&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70549&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70549&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70549&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70549&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70549&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70549&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70549&r=mysqlcfg