This article provides a detailed analysis of the differences between method_exists () and is_callable () in php. For more information, see
This article provides a detailed analysis of the differences between method_exists () and is_callable () in php. For more information, see
What is the difference between the php function method_exists () and is_callable? 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 than the server space. 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 the method exists in the class and the server space, 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 and the website space. If it is private or protected, it returns false.