php is_function() to determine if a variable is a function

Jage picture Jage · May 14, 2010 · Viewed 42.6k times · Source

I was pretty excited to read about anonymous functions in php, which let you declare a variable that is function easier than you could do with create_function. Now I am wondering if I have a function that is passed a variable, how can I check it to determine if it is a function? There is no is_function() function yet, and when I do a var_dump of a variable that is a function::

$func = function(){
    echo 'asdf';
};
var_dump($func);

I get this:

object(Closure)#8 (0) { } 

Any thoughts on how to check if this is a function?

Answer

Jon Benedicto picture Jon Benedicto · May 14, 2010

Use is_callable to determine whether a given variable is a function. For example:

$func = function()
{  
    echo 'asdf';  
};

if( is_callable( $func ) )
{
    // Will be true.
}