What is the difference between PHP function function_exists (), method_exists () and is_callable ()?
First, the next two: 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 advanced in that 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 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.
Next look at a piece of code:
<?PhpClassTest {PublicfunctionA () {Return "Test"; }}Classabc{PublicFunction A ($object,$funcName) {if (! Is_callable (array ( $object, $funcName echo "error:the". " ". $funcName. "No exist in". $object. " </br> ";} else {echo "OK" ; }}} $ABC = new ABC (); $ABC->a ("Test", "111"
Results show
So what happens if the A method in the test class is changed to private or protected?
Code
Results
All right, let's take a look. Method_exists () This function:
Results:
It should be clear when we come to the conclusion.
What is the difference between method_exists () and is_callable () in PHP?
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.
And then there's a function_exists () left,
This is the weakest of the two detection intensity because there is only one parameter function name $string will only judge if the function is defined
To summarize:
Function_exists The simple point is that the judgment function is not defined and Method_exists is the method in the judgment class does not exist is_callable detection parameter is a legitimate callable structure
The return value is all bool
Determine whether a method exists, parse PHP function function_exists (), method_exists () and is_callable () difference