As an example, let's use the is_callable() function which accepts takes a function name as an argument and checks whether or not it can be successfully called from that point in the code.
If I want to check if an object's method is callable, it seems like I have two choices when referencing a static method:
Option 1:
is_callable(array("ObjectName", "MethodName"));
Option 2:
is_callable("ObjectName::MethodName");
(Instance methods can seemingly only be checked using Option 1, passing an object instance as the first array value rather than the string containing the class name.)
Is this just a matter of preference, or syntactic sugar, or is there a solid difference between the two?
It's just syntactic sugar - is_callable("ObjectName::MethodName") looks much nicer, but requires a higher PHP version than the array method.
However, to create a "pointer" to an instance method you must use array($instance, 'MethodName').
From the PHP docs:
// Type 4: Static class method call (As of PHP 5.2.3)
call_user_func('MyClass::myCallbackMethod');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With