Re: [RFC] Array Dereferencing
| From: | Alban LEROUX | Date: | Wed, 09 Jun 2010 13:52:45 +0000 |
| Subject: | Re: [RFC] Array Dereferencing | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-48704@lists.php.net to get a copy of this message | ||
On 2010-06-08 12:41:21 +0200, Johannes Schlüter said:
On Tue, 2010-06-08 at 12:23 +0200, Jacob Oettinger wrote:I think a pretty solution could be to introduce a new magic method like __invoke() at the difference that's a static method not an instance method. Let's me take the name __create() for this example : class Foo { public function __create() { return new Foo(); } public function doSomething() { // ... } } $foo = Foo()->doSomething(); This could be a very nice improvement : - We could chain contruction and method call for classes with lot of setters. - We could make factory design pattern more simplier. - We no more need a name convention for singleton like getInstance(). - It's clean and easy to read. - other ? Personnaly, I actually use this in all of my class. And wrote always code like this : $class = Class::create()->setSomeThing()->doOtherThing(); In fact the uniq problem i see, is could be a name conflict like : function Foo() { // ... } class Foo() { public __create() { // ... } } Foo(); // Instanciate the class or call the function ? I hope this idea can help/inspire you. Regards, -- Alban Leroux seza@paradoxal.org Web developperWould it be equally simple to allow the syntax below? $result = new ResultMaker()->getIt();does this mean$result = new (ResultMaker()->getIt());or$result = (new ResultMaker())->getIt();I assume the later, but that is non-obvious as we allow$result = new $class();and $resultOfFunc = returnsFunc()();Having closures this might make sense. (While I don't want to debug code like $foo("bar")[42]->do()("it"); ) Oh and obviously +1 on the original patch. ;-) johannes