Edit report at http://bugs.php.net/bug.php?id=34502&edit=1
ID: 34502 Comment by: jacob at jacobweber dot com Reported by: goat at daholygoat dot com Summary: method chaining on constructor causes parse error Status: Open Type: Feature/Change Request Package: Feature/Change Request Operating System: Linux PHP Version: 5.0.5 New Comment: 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(); Previous Comments: ------------------------------------------------------------------------ [2010-02-14 23:34:49] 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(); ------------------------------------------------------------------------ [2009-07-18 21:38:04] 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. ------------------------------------------------------------------------ [2005-09-16 10:00:51] 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. ------------------------------------------------------------------------ [2005-09-14 23:25:33] johan...@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-14 21:26:50] der...@php.net I think this is simply not supported right now, so marking as a Feature Request ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/bug.php?id=34502 -- Edit this bug report at http://bugs.php.net/bug.php?id=34502&edit=1