What is the difference between PHP function method_exists () and is_callable ()? In the design process of PHP object-oriented object, we often need to call a certain method to belong to a certain class when the judgment, commonly used methods have method_exists () and is_callable (), in contrast, is_callable () function to a higher level, It accepts the method name as the first argument in the form of a string variable, and returns True if the class method exists and can be invoked. If you want to detect whether a method in a class can be invoked, you can pass an array to the function instead of the class's method name as an argument. The array must contain an object or class name to be its first element, and the method name to be checked as the second element. If the method exists in the class, the function returns True.
code Example:
Copy Code code as follows:
if (is_callable array ($obj, $method))
{
/* The code snippet to operate/*
}
Is_callable () can add another parameter: A Boolean value that, if set to true, simply checks whether the syntax of the given method or function name is correct, without checking whether it really exists. The parameter of the Method_exists () function is an object (or class name) and a method name, which returns true if the given method exists in the object's class
code Example:
Copy Code code as follows:
if (Method_exists ($obj, $method))
{
/* The code snippet to operate/*
}
The difference between PHP functions method_exists () and is_callable () is that in php5, the presence of a method does not mean that it can be invoked. For private,protected and public-type methods, Method_exits () returns True, but is_callable () checks whether there is access to it, if it is private,protected type, It will return false.