What is the difference between PHP function method_exists () and is_callable ()? In the design process of the PHP face object, we often need to make a judgment when calling a method whether it belongs to a class, commonly used methods are method_exists () and is_callable (), in contrast, the is_callable () function is more advanced, It takes the method name in the form of a string variable as the first parameter, and returns True if the class method exists and can be called. If you want to detect whether a method in a class can be called, you can pass an array to the function instead of the method name of the class as a parameter. The array must contain the object or class name as its first element, and the method name to be examined as the second element. If the method exists in the class, the function returns True.
code Example:
Copy CodeThe code is as follows:
if (is_callable (Array ($obj, $method)))
{
/* Code snippet to manipulate */
}
Is_callable () can add another parameter: A Boolean value that, if set to true, simply checks that the syntax of the given method or function name is correct, without checking that it actually exists. The parameter of the Method_exists () function is an object (or class name) and a method name, and returns True if the given method exists in the class of the object.
code Example:
Copy CodeThe code is as follows:
if (Method_exists ($obj, $method))
{
/* Code snippet to manipulate */
}
The difference between 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 methods of the private,protected and public types, Method_exits () returns True, but is_callable () checks to see if they can be accessed if they are of the private,protected type. It will return false.
http://www.bkjia.com/PHPjc/327785.html www.bkjia.com true http://www.bkjia.com/PHPjc/327785.html techarticle What is the difference between PHP function method_exists () and is_callable ()? In the design process of the PHP face object, we often need to make a judgment when invoking whether a method belongs to a class.