ID: 34502 Comment by: spidgorny at gmail dot com Reported By: goat at daholygoat dot com Status: Open Bug Type: Feature/Change Request Operating System: Linux PHP Version: 5.0.5 New Comment:
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. Previous Comments: ------------------------------------------------------------------------ [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 ------------------------------------------------------------------------ [2005-09-14 21:14:57] 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 ';' ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=34502&edit=1