If you want to check if a variable is an anonymous function and not a callable string or array, use instanceof .
$func = function() { echo 'asdf'; }; if($func instanceof Closure) {
Anonymous functions (such as those added in PHP 5.3) are always instances of the Closure class, and each instance of the Closure class is an anonymous function.
In PHP there is another type of thing that can be considered a function, and those objects that implement the __invoke magic method. If you want to include them (while excluding strings and arrays), use method_exists($func, '__invoke') . This will include closures anyway, since closures implement __invoke for consistency.
Brilliand Dec 12 '14 at 23:50 2014-12-12 23:50
source share