The What
Description
The PHP construct call_user_func
calls (invokes) a callback and passes the parameters (if any) to it. You are calling the callback by the full qualified name given to it in the first parameter. If the callback returns a value, then that value is returned by call_user_func
.
Watch these videos first
Before you tackle call_user_func
, make sure you first watch callable and callback. call_user_func
works on both of these constructs.
Parameters
callback
- The callable to be called (invoked).
parameter
- Parameter(s) to be passed to the callback. Specify them by the needs of the callback, i.e. based on its parameter list.
Return Values
If the callback returns a value, then call_user_func
returns that value; else it returns NULL
. On error, it returns FALSE
.
Show It in ActionBasic
Let’s see the call_user_func
function in action. In this video, you will see how it works.
Keep It Simple, Stupid (KISS) - the best kiss you'll get in code.
Go DeepPro
In this video, you will go deeper into the instruction.
Calling MethodsPro
How do you call object and static methods with call_user_func
? How do you call closures?
See It In Popular TechnologiesPro
Let’s look at call_user_func
in Laravel, Symfony, and WordPress. You will see how prevalent it is used.
WhenPro
When do you use call_user_func
versus one of the other ways to call a callback?