Parse the difference between PHP functions method_exists () and is_callable () _php Skills

Source: Internet
Author: User
What is the difference between PHP function method_exists () and is_callable ()? In the design process of PHP object-oriented object, we often need to call a certain method to belong to a certain class when the judgment, commonly used methods have method_exists () and is_callable (), in contrast, is_callable () function to a higher level, 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 invoked. If you want to detect whether a method in a class can be invoked, you can pass an array to the function instead of the class's method name as an argument. The array must contain an object or class name to be 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 code as follows:

if (is_callable array ($obj, $method))
{
/* The code snippet to operate/*
}

Is_callable () can add another parameter: A Boolean value that, if set to true, simply checks whether the syntax of the given method or function name is correct, without checking whether it really exists. The parameter of the Method_exists () function is an object (or class name) and a method name, which returns true if the given method exists in the object's class
code Example:
Copy Code code as follows:

if (Method_exists ($obj, $method))
{
/* The code snippet to operate/*
}

The difference between PHP functions method_exists () and is_callable () is that in php5, the presence of a method does not mean that it can be invoked. For private,protected and public-type methods, Method_exits () returns True, but is_callable () checks whether there is access to it, if it is private,protected type, It will return false.

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.