Parsing the difference between PHP function method_exists () and is_callable () _php Tutorial

Source: Internet
Author: User
What is the difference between PHP function 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 more advanced, It takes the method name in the form of a string variable as the first parameter, 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.
code Example:
Copy CodeThe code is as follows:
if (is_callable (Array ($obj, $method)))
{
/* Code snippet to manipulate */
}

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 parameter of the Method_exists () function is an object (or class name) and a method name, and returns True if the given method exists in the class of the object.
code Example:
Copy CodeThe code is as follows:
if (Method_exists ($obj, $method))
{
/* Code snippet to manipulate */
}

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.

http://www.bkjia.com/PHPjc/327785.html www.bkjia.com true http://www.bkjia.com/PHPjc/327785.html techarticle What is the difference between PHP function method_exists () and is_callable ()? In the design process of the PHP face object, we often need to make a judgment when invoking whether a method belongs to a class.

  • 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.