call_user_func_array
Description
The call_user_func_array of Function Handling for PHP calls a callback with an array of parameters.
Syntax
call_user_func_array(
callable $callback,
array $args
): mixedParameters
callback
The callable to be called.
args
The parameters to be passed to the callback, as an array.
If the keys of args are all numeric, the keys are ignored and each element will be passed to callback as a positional argument, in order.
If any keys of args are strings, those elements will be passed to callback as named arguments, with the name given by the key.
It is a fatal error to have a numeric key in args appear after a string key, or to have a string key that does not match the name of any parameter of callback.
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 =
[
"Hello",
"World!"
];
$return = call_user_func_array($callback, $args);
echo $return;
myfunction Hello World!
2 · class
<?
class myclass
{
static function myfunction($myparameter1, $myparameter2)
{
return __METHOD__. " $myparameter1 $myparameter2";
}
}
$callback = "myclass::myfunction";
$args =
[
"Hello",
"World!"
];
$return = call_user_func_array($callback, $args);
echo $return;
myclass::myfunction Hello World!
3 · class
<?
class myclass
{
static function myfunction($myparameter1, $myparameter2)
{
return __METHOD__. " $myparameter1 $myparameter2";
}
}
$callback =
[
"myclass",
"myfunction"
];
$args =
[
"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 =
[
new myclass(),
"myfunction"
];
$args =
[
"Hello",
"World!"
];
$return = call_user_func_array($callback, $args);
echo $return;
myclass::myfunction Hello World!
5 · namespace
<?
namespace mynamespace;
class myclass
{
static function myfunction($myparameter1, $myparameter2)
{
return __NAMESPACE__. " $myparameter1 $myparameter2";
}
}
$callback = __NAMESPACE__. "\myclass::myfunction";
$args =
[
"Hello",
"World!"
];
$return = call_user_func_array($callback, $args);
echo $return;
mynamespace Hello World!
6 · namespace
<?
namespace mynamespace;
class myclass
{
static function myfunction($myparameter1, $myparameter2)
{
return __NAMESPACE__. " $myparameter1 $myparameter2";
}
}
$callback =
[
__NAMESPACE__. "\myclass",
"myfunction"
];
$args =
[
"Hello",
"World!"
];
$return = call_user_func_array($callback, $args);
echo $return;
mynamespace Hello World!
7 · reference
<?
function myfunction(&$myparameter1, &$myparameter2)
{
$myparameter1 = "Hello";
$myparameter2 = "World!";
echo __FUNCTION__. " $myparameter1 $myparameter2";
}
$arg3 = "3";
$arg4 = "4";
$callback = "myfunction";
$args =
[
&$arg3,
&$arg4
];
call_user_func_array($callback, $args);
echo "\nglobal $arg3 $arg4";
myfunction Hello World! global Hello World!