__invoke() Method¶
The __invoke() magic method is called when the code calls an object as a function name. This type of class is also called a functor.
The method is able to take an arbitrary number of arguments, as usual.
When a class has the __invoke class, it is possible to can a method without mentioning its name: the object is the method.
__invoke is also considered as the anonymous method: it is called without using its name.
<?php
// extracted from the PHP manual
class CallableClass
{
public function __invoke($x)
{
var_dump($x);
}
}
$obj = new CallableClass;
$obj(5);
var_dump(is_callable($obj));
?>
See also PHP __invoke, The Magical invoke Method in PHP. Your Clean Code Ally and Why PHP’s __invoke Might Be Your Best Kept Secret!.
Related : Magic Methods, SOLID, Anonymous, Functor, Callables, Invoke, New Without Parenthesis, Anonymous Function