Can I check if the callback is valid?

I want to be able to check if the callback is valid before I try to call it. Is it 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 .

+4
source share
1 answer

You need the is_callable() function.

From the PHP Manual on is_callable () :

Make sure that the contents of the variable can be called a function. This can ensure that a simple variable contains the name of the actual function or the array contains the correctly encoded name of the object and function.

(and it also works great with closure)

+11
source

All Articles