On 17/03/2013, at 8:33 AM, 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


Function and class names can overlap. So:

function a() { return "A"; }
class A {}

is valid.
---
Simon Welsh
Admin of http://simon.geek.nz/


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

Reply via email to