On Sat, Mar 16, 2013 at 9:33 PM, Pierre du Plessis
<pie...@pcservice.co.za> wrote:
> On Mar 16, 2013 9:35 PM, "Daniele Orlando" <dnl.r...@gmail.com> wrote:
>>
>> Hi List,
>>
>> I'm interested in proposing an RFC and I would know your opinion.
>>
>> === Current Situation ===
>> Since PHP 5.3 we can use an object instance, who defines the __invoke()
>> method, as a callable object.
>> Example:
>>
>> // PHP Code.
>> class Runnable
>> {
>>     public function __invoke()
>>     {
>>         echo "Runned";
>>     }
>> }
>>
>> $r = new Runnable();
>> $r();
>>
>> // Output
>> Runned
>>
>> === The Idea ===
>> In Python, when you construct an object, you don't need to use the "new"
>> keyword but you just invoke the class name followed by "()", like the
> class
>> is a function.
>> Example:
>>
>> // Python Code.
>> class A:
>>     pass
>>
>> A()
>>
>> // Output.
>> <__main__.A instance at %address>
>>
>> Now, would be interesting to extend the PHP __invoke() method adding an
>> __invokeStatic() method, like happens with __call() and __callStatic()
>> methods.
>> In this way could be possible to use a class name to invoke the
>> __invokeStatic() method.
>> Example:
>>
>> // PHP Code.
>> class TrueRunnable
>> {
>>     public static function __invokeStatic()
>>     {
>>         echo "Runned";
>>     }
>> }
>>
>> TrueRunnable();
>>
>> // Output.
>> Runned
>>
>> But the possibility are endless:
>>
>> class A
>> {
>>     public static function __invokeStatic()
>>     {
>>         return new A();
>>     }
>>     public method m() {}
>> }
>>
>> A()->m();
>>
>> // or
>>
>> class A
>> {
>>     private $_instance;
>>     public static function __invokeStatic()
>>     {
>>         // Singleton pattern.
>>         if (self::$_instance) {
>>             return self::$_instance;
>>         }
>>
>>         return self::$_instance = new A();
>>     }
>>     public method m() {}
>> }
>>
>> A()->m();
>>
>>
>> === Conclusion ===
>> This feature makes the __invoke() method consistent with the __call() and
>> __callStatic() methods,
>> and opens the door to many cool stuff.
>>
>> Any feedback is appreciated.
>>
>> Daniele Orlando
>
> I don't really see a use case for this, as you can already use the syntax
> A::method();
>
> E.G class A { public static function invoke() { return new A; }
>
> public function m() { echo 'Runned'; }
>
> A::invoke()->m();
>
> Your example above only saves a few characters to type and can lead to a
> lot of problems if you have a function with the same name as the class.

Using A::invoke(), you need to know the name of "invoke()" and it's
hard to force users always to use the invoke() function.
Using A() would be more clean since all the static init(), factory(),
invoke(), getInstance() are gone.
Having __call(), __callStatic(), __invoke() and invokeStatic() would
make the overloading concept more consistent.

Regards,
Thomas

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

Reply via email to