The specific differences between is_callable () and method_exists () functions in PHP are described

Source: Internet
Author: User

In many product applications, we can often see the following usage, which is used to check whether a method exists in an object.

<?phpif (method_exists ($object, ' SomeMethod ')) {       $object->somemethod ($this, TRUE);}? >

The purpose of this code is relatively easy to understand, there is an object for $object, we want to know if it has a method for SomeMethod, if there is, call this method.

This code looks correct and will work well for most of the time. But if the method of the $object object is not visible to the current operating environment, does the program still work? Just as this function name method exists, it simply checks to see if there is a method we expect for the class or object we provide, and if so, returns true if not, and returns false if not, and there is no problem with visibility. So, when you happen to judge a private or protected method, you get a correct return, but when you do, you get a "Fatal error" warning.

The real intent of the above code should be understood as: whether we can invoke its SomeMethod method in the current scope for the provided class or object. And that's what is_callable() the function exists for. The is_callable() function receives a callback parameter that can specify a function name or an array containing the method name and object, which returns true if it can be executed in the current scope.

<?phpif (is_callable (Array ($object, ' SomeMethod ')) {      $object->somemethod ($this, TRUE);}?>

Here's an example to illustrate the difference between the two.

<?phpclass Foo {public    function Publicmethod () {}        Private function Privatemethod () {} public        static function Publicstaticmethod () {}        private static function Privatestaticmethod () {}} $foo = new foo (); $callbacks = array (    Array ($foo, ' Publicmethod '),    Array ($foo, ' Privatemethod '),    Array ($foo, ' Publicstaticmethod '),    Array ($foo, ' Privatestaticmethod '),    array (' foo ', ' Publicmethod '),    array (' foo ', ' Privatemethod '    ), Array (' foo ', ' Publicstaticmethod '),    array (' foo ', ' Privatestaticmethod ')   ; foreach ($callbacks as $ Callback) {    var_dump ($callback);    Var_dump (method_exists ($callback [0], $callback [1]);    Var_dump (Is_callable ($callback));        Echo str_repeat ('-', ten);        echo ' <br/> ';}

After executing the above script, we will clearly see the difference between the two functions.

is_callable()There are other uses, such as not checking the provided class or method, and checking only the correct syntax of the function or method. Like method_exists() , is_callable() you can trigger automatic loading of classes.

If an object has a magic method, it returns __call False when the method is judged, method_exists() and is_callable() returns True.

<?phpclass methodtest {public    function __call ($name, $arguments) {      echo ' calling object method '. $name. ". Implode (', ', $arguments);      echo ' <br/> ';    }} $obj = new Methodtest (), $obj->runtest (' in object context '), Var_dump (Method_exists ($obj, ' runtest ')), Var_dump (is_ Callable (Array ($obj, ' runtest ')); Echo ' <br/> ';

Run results

Calling object method Runtest in object context
BOOL (FALSE) bool (true)

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.