What is the difference between the php function method_exists() and is_callable()? In the PHP object-oriented design process, we often need to make a judgment when calling a method whether it belongs to a certain class. Commonly used methods include method_exists() and is_callable(). In comparison, the is_callable() function is more advanced. Some, which accept the method name in the form of a string variable as the first argument, return true if the class method exists and can be called. If you want to check whether a method in a class can be called, you can pass an array to the function as a parameter instead of the method name of the class. The array must contain the object or class name as its first element and the method name to be checked as the second element. If the method exists in the class, the function returns true.
Code example:
Copy code The code is as follows:
if ( is_callable( array( $obj, $method ) ) )
{
/*Code segment to be operated*/
}
is_callable() can add 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, without checking whether it actually exists. The parameters of the method_exists() function are an object (or class name) and a method name. If the given method exists in the object's class, it returns true
Code example:
Copy code The code is as follows:
if ( method_exists( $obj, $method ) )
{
/*To be operated Code snippet */
}
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 methods of private, protected and public types, method_exits() will return true, but is_callable() will check whether it exists and can be accessed. If it is of private, protected type, it will return false.
http://www.bkjia.com/PHPjc/327785.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327785.htmlTechArticleWhat is the difference between php function method_exists() and is_callable()? In the PHP object-oriented design process, we often need to make a judgment when calling a certain method whether it belongs to a certain class...
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn