Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I test if a callback is valid?

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.

like image 242
Beau Simensen Avatar asked Apr 21 '11 15:04

Beau Simensen


1 Answers

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)

like image 148
kapa Avatar answered Sep 30 '22 04:09

kapa