php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34502 method chaining on constructor causes parse error
Submitted: 2005-09-14 21:14 UTC Modified: 2012-09-23 16:34 UTC
Votes:16
Avg. Score:4.7 ± 0.6
Reproduced:15 of 15 (100.0%)
Same Version:4 (26.7%)
Same OS:9 (60.0%)
From: goat at daholygoat dot com Assigned: nikic (profile)
Status: Closed Package: *General Issues
PHP Version: 5.0.5 OS: Linux
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: goat at daholygoat dot com
New email:
PHP Version: OS:

 

 [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 ';'

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-14 21:26 UTC] derick@php.net
I think this is simply not supported right now, so marking as a Feature Request
 [2005-09-14 23:25 UTC] johannes@php.net
By reading the code I'd expect that A is some function    
returning an object. returnStr() being a method of that    
object returning a class name used for new. (Somehow a    
combination of "new $a;" and a simple   
"function_call()->methodCallOnReturnedObject()" which is   
possible since PHP 5) I would like some syntax like this,   
too - but thinking about it I see too much confusion and   
didn't find a nice solution which is clear when reading   
code.   
   
I set this to bogus since I think it's too much confusion,  
but if you have a nice and clear syntax feel free to  
re-open it - I'd be happy, but don't see how this is  
possible without logic conflicts :-)  
 [2005-09-16 10:00 UTC] goat at daholygoat dot com
@Johannes:
I don't really get your interpretion of the problem. A() is of course the constructor (A() in A). The constructor returns an object of type A. returnStr() is a method of A, so when calling returnStr() on a new A(), it should invoke returnStr() on a new object of A. For example, in Java it's fine to do this:
System.out.println(new Object().toString());
Which makes sense because when you _can_ do method chaining (which you can in PHP5), there are many times where you just want to call one chain on a new object, instead of seperately instantiating the class.
So I have to go with Derick pointing out it's simply not supported right now.
 [2009-07-18 21:38 UTC] spidgorny at gmail dot com
Here's the ugly trick how to do object instantiation and chaining in one line:

	$view->loginForm = end($_ = array(
		$l = new Login(),
		$l->render()->chain()->everything()->you()->like()
	));

$_ and $l are two unnecessary variables. I told you - it's ugly.

Anybody can make it better?
Any ETA for implementing it in PHP directly?
Hello visitor. Please vote.
 [2010-02-14 23:34 UTC] jaka at kubje dot org
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();
 [2010-06-15 21:05 UTC] jacob at jacobweber dot com
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();
 [2012-09-23 16:34 UTC] nikic@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: nikic
 [2012-09-23 16:34 UTC] nikic@php.net
Closing as PHP 5.4 supports with with the (new A)->b() syntax.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 16:01:31 2024 UTC