This article provides a detailed analysis of the differences between method_exists () and is_callable () in php. For more information, see method_exists () and is_callable () in php () what is the difference? In the design process of php surface objects, we often need to make a judgment when calling a method to determine whether it belongs to a class. commonly used methods include method_exists () and is_callable (), in contrast, the is_callable () function is more advanced. it accepts the method name in the form of string variables as the first parameter. if the class method exists and can be called, true is returned. If you want to check whether a method in the class can be called, you can pass an array to the function instead of the method name of the class as the parameter. The array must contain the object or class name to use it as its first element, and the method name to be checked is used as the second element. If this method exists in the class, the function returns true.
Sample code:
The code is as follows:
If (is_callable (array ($ obj, $ method )))
{
/* Code segment to be operated */
}
Is_callable () can be added with another parameter: a Boolean value. If this parameter is set to true, the function only checks whether the syntax of the given method or function name is correct, and does not check whether it actually exists. The parameter of the method_exists () function is an object (or class name) and a method name. if the given method exists in the class of the object, true is returned.
Sample code:
The code is as follows:
If (method_exists ($ obj, $ method ))
{
/* Code segment to be operated */
}
The difference between the php function method_exists () and is_callable () is that in php5, the existence of a method does not mean that it can be called. For private, protected, and public methods, method_exits () returns true, but is_callable () checks whether it can be accessed. if it is private or protected, it returns false.