I'd like to be able to test to see whether or not a callback is valid before I try to call it. Is this possible?
If I call call_user_func
or call_user_func_array
with something like array($this, 'methodThatDoesNotExist')
PHP warns with [E_WARNING] call_user_func() expects parameter 1 to be a valid callback
.
What you need is the is_callable()
function.
From the PHP Manual on is_callable():
Verify that the contents of a variable can be called as a function. This can check that a simple variable contains the name of a valid function, or that an array contains a properly encoded object and function name.
(and it also works fine with closures)
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