PHP Callable Class
__invoke
makes an object callable.
<?php
class CallableObject
{
public function __invoke()
{
}
}
?>
Though there is no magic method that makes a class callable e.g.
__invokeStatic
.
But we can still make a callable class. That’s to define a function with the same name as the class. E.g.
<?php
function CallableClass()
{
return new CallableClass();
}
class CallableClass
{
public function __invoke()
{
}
}
?>
Note that before PHP 5.6, the constructor signature has to duplicate again as the function signature, this makes it a little bit awakard to define. (Using Reflection won’t be easier either) E.g.
<?php
function CallableClassPhp53($lifeTime)
{
return new CallableClassPhp53($lifeTime);
}
class CallableClassPhp53
{
public function __construct($lifeTime)
{
}
}
?>
This is solved by the introduction of Variable-length argument lists since PHP 5.6.
<?php
function CallableClassPhp56()
{
return new CallableClassPhp56(...func_get_args());
}
class CallableClassPhp56
{
public function __construct($lifeTime)
{
}
}
?>