call_user_func_array
Description
The call_user_func_array of Function Handling for PHP call a callback with an array of parameters.
Syntax
call_user_func_array(callable $callback, array $args): mixed
Parameters
callback
The callable to be called.
args
The parameters to be passed to the callback, as an indexed array.
Return
Returns the return value of the callback, or false on error.
Examples
1 · callback args
<? function myfunction($myparameter1, $myparameter2) { return __FUNCTION__ . " $myparameter1 $myparameter2"; } $callback = "myfunction"; $args = array("Hello", "World!"); $return = call_user_func_array($callback, $args); echo $return;
myfunction Hello World!
2 · closure
<? $myfunction = function($myparameter1, $myparameter2) { return __FUNCTION__ . " $myparameter1 $myparameter2"; }; $callback = $myfunction; $args = array("Hello", "World!"); $return = call_user_func_array($callback, $args); echo $return;
{closure:/home/osbocom/public_html/php/demo/index.php:3} Hello World!
3 · class
<? class myclass { static function myfunction($myparameter1, $myparameter2) { return __METHOD__ . " $myparameter1 $myparameter2"; } } $callback = "myclass::myfunction"; $args = array("Hello", "World!"); $return = call_user_func_array($callback, $args); echo $return;
myclass::myfunction Hello World!
4 · class
<? class myclass { static function myfunction($myparameter1, $myparameter2) { return __METHOD__ . " $myparameter1 $myparameter2"; } } $callback = array("myclass", "myfunction"); $args = array("Hello", "World!"); $return = call_user_func_array($callback, $args); echo $return;
myclass::myfunction Hello World!
5 · class
<? class myclass { static function myfunction($myparameter1, $myparameter2) { return __METHOD__ . " $myparameter1 $myparameter2"; } } $callback = array(new myclass(), "myfunction"); $args = array("Hello", "World!"); $return = call_user_func_array($callback, $args); echo $return;
myclass::myfunction Hello World!
6 · namespace
<? namespace mynamespace; class myclass { static function myfunction($myparameter1, $myparameter2) { return __NAMESPACE__ . " $myparameter1 $myparameter2"; } } $callback = __NAMESPACE__ . "\myclass::myfunction"; $args = array("Hello", "World!"); $return = call_user_func_array($callback, $args); echo $return;
mynamespace Hello World!
7 · namespace
<? namespace mynamespace; class myclass { static function myfunction($myparameter1, $myparameter2) { return __NAMESPACE__ . " $myparameter1 $myparameter2"; } } $callback = array(__NAMESPACE__ . "\myclass", "myfunction"); $args = array("Hello", "World!"); $return = call_user_func_array($callback, $args); echo $return;
mynamespace Hello World!
8 · reference
<? function myfunction(&$myparameter1, &$myparameter2) { $myparameter1 = "Hello"; $myparameter2 = "World!"; echo __FUNCTION__ . " $myparameter1 $myparameter2"; } $arg3 = "3"; $arg4 = "4"; $callback = "myfunction"; $args = array(&$arg3, &$arg4); call_user_func_array($callback, $args); echo "\nglobal $arg3 $arg4";
myfunction Hello World! global Hello World!