On 2010-06-08 12:41:21 +0200, Johannes Schlüter said:

On Tue, 2010-06-08 at 12:23 +0200, Jacob Oettinger wrote:
Would 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


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
s...@paradoxal.org
Web developper


--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to