|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2007-02-01 09:27 UTC] derick@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Oct 27 12:00:02 2025 UTC |
Description: ------------ Calling a function on a newly created object requires saving the object to a variable. Script that clarifies what I'm trying to say: class C { public function __construct() {} public function f() { return 1234; } }; $y = (new C())->f(); print($y); The above doesn't work. However, it does work if you replace the penultimate line with $x = new C(); $y = $x->f(); If we add parentheses around the $x, i.e. $y = ($x)->f(); then it still doesn't work. This and bug report #9587 indicate that the actual underlying cause is that object-valued variables are implemented, but object-valued expressions are not. Although the "this is not a bug" comment on the above-cited bug report #9587 did not include a rationale, it's difficult for me to decide whether it applies here as well; maybe this bug report should actually be a feature request? Personally, as a PHP rookie but Java veteran, I would say that object-valued expressions SHOULD work, and they DON'T, therefore it's a BUG. Thanks! Reproduce code: --------------- class C { public function __construct() {} public function f() { return 1234; } }; $y = (new C())->f(); print($y); Expected result: ---------------- 1234 Actual result: -------------- Parse error: parse error, unexpected T_OBJECT_OPERATOR in c:\path\test.php on line 5