This article introduces the difference between method_exists () and the is_callable () function in PHP, and it is necessary for a friend to refer to it. Understand the difference between PHP function method_exists () and is_callable ()? In the design process of the PHP face object, it is often necessary to make a judgment when calling a method whether it belongs to a certain 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. Having said so much, look at the following example:
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 arguments to the method_exists () function are an object (or class name) and a method name, which returns true if the given method exists in the class of the object. Example:
Description: The difference between PHP function method_exists () and is_callable (), in PhP5, a method exists 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. So much has been introduced, hoping to help you understand the difference between the two functions. |