Analysis of the difference between method_exists () and is_callable () function in PHP

Source: Internet
Author: User
This article introduces the difference between method_exists () and the is_callable () function in PHP, and it is necessary for a friend to refer to it.

Understand the difference between PHP function method_exists () and is_callable ()? In the design process of the PHP face object, it is often necessary to make a judgment when calling a method whether it belongs to a certain class, commonly used methods are method_exists () and is_callable ().

In contrast, the is_callable () function is advanced in that 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 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.

Having said so much, look at the following example:

 
      

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 arguments to the method_exists () function are an object (or class name) and a method name, which returns true if the given method exists in the class of the object.

Example:

 
      

Description: The difference between PHP function method_exists () and is_callable (), in PhP5, a method exists 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.

So much has been introduced, hoping to help you understand the difference between the two functions.

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