Differences between php functions method_exists () and is_callable ()
Source: Internet
Author: User
Differences between php functions method_exists () and is_callable (): What is the difference between php functions 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 php functions method_exists () and is_callable () difference
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. 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 this method exists in the class, the function returns true.
Sample code:
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:
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. if it is private or protected, it returns false. Brand website construction: http://www.ucantech.com/(fblww-0111)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.