Beau Simensen
Beau Simensen

Reputation: 4578

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.

Upvotes: 5

Views: 1996

Answers (1)

kapa
kapa

Reputation: 78671

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)

Upvotes: 13

Related Questions