The difference between PHP function method_exists () and is_callable ()

Source: Internet
Author: User
Tags vars
The difference between PHP function method_exists () and is_callable ()

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:

?



    1. if ? (? is_callable (? Array (? $obj ,? $method ?)?)?) ??
    2. { ??
    3. ???? / * Code snippet to manipulate * / ??
    4. }??

?

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:

?



    1. if ? (? method_exists (? $obj ,? $method ?)?) ??
    2. { ??
    3. ???? / * Code snippet to manipulate * / ??
    4. }??

?

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.souebooks.com/14.htm

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