PHP Reference ยท Functions and Classes
PHP call_user_func() and call_user_func_array()
Reference for PHP call_user_func() and call_user_func_array(): call any callable with arguments, pass named arguments, and when the ... spread syntax is better.
call_user_func() calls a callable and passes it the extra arguments you give. call_user_func_array() does the same but takes the arguments as one array, which is useful when you build the list at run time.
Since PHP 7 you can often call a callable directly: $callback(1, 2) and $callback(...$args) do the same job with less typing. These two functions remain common in older code and are still handy when the callable is a string such as 'Class::method'.
echo call_user_func('str_repeat', 'ab', 3); // ababab echo call_user_func_array('max', [4, 9, 2]); // 9
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
call_user_func(callable $callback, mixed ...$args): mixed call_user_func_array(callable $callback, array $args): mixed
| Name | Description |
|---|---|
$callback |
Any callable: function name, [object, 'method'], 'Class::method', closure, invokable object. |
$args |
For call_user_func(): the arguments, as separate parameters. For call_user_func_array(): an array. |
Return value: whatever the callback returns.
Errors: a value that is not callable throws TypeError (PHP 8). Too few arguments for the target function throws ArgumentCountError.
In call_user_func_array(), a list array supplies positional arguments, and string keys are passed as named arguments (PHP 8.0+). Mixing is allowed, but positional entries must come first.
Usage notes
- Direct call is usually shorter.
$fn(1, 2)and$fn(...$args)work for every callable form, including[$obj, 'method']and'Class::method'arrays and strings. - References are not passed.
call_user_funchands values to the target, so a by-reference parameter receives a copy and PHP emits a warning; incall_user_func_array()you can pass&$xinside the array to keep the reference. - Overhead. Both functions are slightly slower than a direct call; do not use them in very hot loops when a direct call is possible.
- Dynamic dispatch pattern. Mapping names to callables in an array (as above) is safer than building function names from strings.
- Never pass user input as the callback.
call_user_func($_GET['fn'])allows an attacker to run any function. Whitelist allowed names. - Late static binding.
call_user_func('parent::method')and'static::method'forms are deprecated; useparent::method()or closures. - Named arguments with
call_user_func()are supported only through extra named arguments after the callback, which are forwarded to the callback (PHP 8.0+). - Arrow functions and first-class syntax reduce the need for these helpers:
$obj->method(...)returns a closure you can call directly.