Send a blank email to [email protected] to get a copy of this message
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