Difference between parsing php functions method_exists () and is_callable ()

Source: Internet
Author: User
This article provides a detailed analysis of the differences between method_exists () and is_callable () in php. For more information, see

This article provides a detailed analysis of the differences between method_exists () and is_callable () in php. For more information, see

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 than the server space. 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 the method exists in the class and the server space, the function returns true.
Sample Code:

The Code is as follows:


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:

The Code is as follows:


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 and the website space. If it is private or protected, it returns false.
Related Article

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.