What is the difference between a method reference as an array with respect to a string?

As an example, you can use a function is_callable()that takes a function name as an argument and checks whether it can be successfully called from this point in the code.

If I want to check if an object's method is callable, it seems like I have two options when referencing a static method:

Option 1:

 is_callable(array("ObjectName", "MethodName"));

Option 2:

 is_callable("ObjectName::MethodName");

(Instance methods can apparently only be checked using option 1, passing the instance of the object as the first value of the array, rather than a string containing the class name.)

Is it just a matter of preference, or syntactic sugar, or is there a solid difference between the two?

+3
source share
1 answer

- is_callable("ObjectName::MethodName") , PHP, .

"" array($instance, 'MethodName').

PHP:

// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');
+7

Source: https://habr.com/ru/post/1787887/


All Articles