invoke

suggest change

__invoke runs when the object is called as a function. That is useful for callbacks and for small function-like objects.

class Invokable
{
    /**
     * This method will be called if object will be executed like a function:
     *
     * $invokable();
     *
     * Args will be passed as in regular method call.
     */
    public function __invoke(...$args): void
    {
        print_r(func_get_args());
    }
}

// Example:
$invokable = new Invokable();
$invokable(1, 2, 3);

// outputs:
# Array
# (
#     [0] => 1
#     [1] => 2
#     [2] => 3
# )

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents