Type hinting – Difference between `Closure` and `Callable`

The difference is, that a Closure must be an anonymous function, where callable also can be a normal function.

You can see/test this with the example below and you will see that you will get an error for the first one:

function callFunc1(Closure $closure) {
    $closure();
}

function callFunc2(Callable $callback) {
    $callback();
}

function xy() {
    echo 'Hello, World!';
}

callFunc1("xy"); // Catchable fatal error: Argument 1 passed to callFunc1() must be an instance of Closure, string given
callFunc2("xy"); // Hello, World!

So if you only want to type hint anonymous function use: Closure and if you want also to allow normal functions use callable as type hint.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)