PHP's is_callable is used to detect whether a function or a class method can be executed, in official documents, to detect whether the parameter is a legitimate callable structure, as shown in the code.
Class A
{public
function __construct () {
echo ' Class: '. Get_called_class ();
The public
function cc () {
$a = 3;
return $a;
}
}
$a = new A;
$test = is_callable (Array (' A ', ' cc '));
Var_dump ($test); exit;
Printed result:
class:a
Boolean True
Official Example:
function someFunction ()
{
}
$functionVariable = ' someFunction ';
Var_dump (Is_callable ($functionVariable, False, $callable _name)); BOOL (TRUE)
echo $callable _name, "\ n"; SomeFunction////
Array containing a method
//
class SomeClass {
functi On SomeMethod ()
{
}
}
$anObject = new SomeClass ();
$methodVariable = Array ($anObject, ' SomeMethod ');
Var_dump (Is_callable ($methodVariable, True, $callable _name)); BOOL (TRUE)
echo $callable _name, "\ n"; Someclass::somemethod
The official example is a detection function, a method for detecting classes, and a more specific usage.
Example
The demo code is as follows:
<?php
class Test {
static function A () {return
"Test";
}
}
Class ABC {public
function A ($Object, $funcName) {
if (!is_callable ($Object, $funcName)) {//here Array ($ Object, $funcname), equivalent to $object:: $funcname invoke $object's $funcname method
echo ("Error");
}else {
echo ("OK");
}
}
$abc =new ABC ();
$ABC->a ("Test", "AA"), there is no AA method under//test class, output error
$abc->a ("Test", "a"),//test class has a method, output OK
?>
OK, the use of this function