Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

__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 method, it is possible to call a method on its objects without mentioning a method’s name: the object itself 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($trunk)
        {
            var_dump($trunk);
        }
    }

    $obj = new CallableClass;
    $obj(5);
    var_dump(is_callable($obj));

?>

Documentation

See Also