i want to call function store inside array but before i do it i want to check if it is function,string or any other type.
please take a look at my code
$a=new stdClass();
$array = array(function() {
return "Bhavik Patel";
}, "1213",$a);
foreach ($array as $key => $value) {
if (is_object($value)) {
echo $value() . "<br/>";
} else {
echo $value . "<br/>";
}
}
by doing this i can check if value is object then i call it but if i pass object it gives (pf course this will give error)
my intention is to find if value is function then call it.
To check specifically for anonymous functions you can test the value against \Closure
like so:
if ($value instanceof \Closure) {
echo $value(), "
";
} else {
echo $value;
}
The problem with is_callable()
is that if your value is "explode"
, it will return true
which is obviously not what you want.
is_callable() will help you, but notice that if you pass an array like this
array('object or class','method')
is_callable() return true, so you'd better to check it isn't an array
if (is_callable($value) && !is_array($value)) {.....}
see is_callable
Try [is_callable][1]
in PHP. I have tested it, it works for me.