|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-09-14 21:14 UTC] goat at daholygoat dot com
Description:
------------
When doing method chaining on a constructor (without seperately instantiating the object first), a parse error occurs.
Reproduce code:
---------------
class A
{
private $str;
function A($str)
{
$this->str = $str;
}
function returnStr()
{
return $str;
}
}
echo new A("hello")->returnStr();
Expected result:
----------------
The reference to an object of A created with A's constructor would allow me to call returnStr() on it.
Actual result:
--------------
I'm getting a parse error.
PHP Parse error: parse error, unexpected T_OBJECT_OPERATOR, expecting ',' or ';'
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 20:00:01 2025 UTC |
You're complicating things too much. You can solve this by simply making 'new' bind stronger than '->'. And even if it doesn't, this should still work: (new A('foo'))->someMethod();Here's a workaround: use a static factory method: class A { public static function create($str) { return new A($str); } ... } echo A::create("hello")->returnStr();