PHP 8.3.0 RC 6 available for testing

call_user_func_array

(PHP 4 >= 4.0.4, PHP 5, PHP 7, PHP 8)

call_user_func_array调用回调函数,并把一个数组参数作为回调函数的参数

说明

call_user_func_array(callable $callback, array $args): mixed

把第一个参数作为回调函数(callback)调用,把参数数组作(args)为回调函数的的参数传入。

参数

callback

被调用的回调函数。

args

要被传入回调函数的数组。

如果 args 的 key 都是数字,则会忽略 key,并按顺序将每个元素作为位置参数传递给 callback

如果 args 的任一 key 是字符串,则这些元素将作为命名参数传递给 callback,名称由 key 指定。

args 中,如果数字 key 在 字符串 key 之后出现,或者字符串 key 与 callback 的任何参数名称不匹配,将会导致 fatal error。

返回值

返回回调函数的结果。如果出错的话就返回 false

更新日志

版本 说明
8.0.0 args 键现在将被解释为参数名称,而不是被默默地忽略。

示例

示例 #1 call_user_func_array()例子

<?php
function foobar($arg, $arg2) {
echo
__FUNCTION__, " got $arg and $arg2\n";
}
class
foo {
function
bar($arg, $arg2) {
echo
__METHOD__, " got $arg and $arg2\n";
}
}


// Call the foobar() function with 2 arguments
call_user_func_array("foobar", array("one", "two"));

// Call the $foo->bar() method with 2 arguments
$foo = new foo;
call_user_func_array(array($foo, "bar"), array("three", "four"));
?>

以上示例的输出类似于:

 
foobar got one and two
foo::bar got three and four

示例 #2 call_user_func_array()使用命名空间的情况

<?php

namespace Foobar;

class
Foo {
static public function
test($name) {
print
"Hello {$name}!\n";
}
}

call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));

call_user_func_array(array(__NAMESPACE__ .'\Foo', 'test'), array('Philip'));

?>

以上示例的输出类似于:

 
Hello Hannes!
Hello Philip!

示例 #3 把完整的函数作为回调传入call_user_func_array()

<?php

$func
= function($arg1, $arg2) {
return
$arg1 * $arg2;
};

var_dump(call_user_func_array($func, array(2, 4)));

?>

以上示例会输出:

 
int(8)

示例 #4 传引用

<?php

function mega(&$a){
$a = 55;
echo
"function mega \$a=$a\n";
}
$bar = 77;
call_user_func_array('mega',array(&$bar));
echo
"global \$bar=$bar\n";

?>

以上示例会输出:

 
function mega $a=55
global $bar=55

示例 #5 call_user_func_array() 使用命名参数

<?php
function foobar($first, $second) {
echo
__FUNCTION__, " got $first and $second\n";
}

// Call the foobar() function with named arguments in non-positional order
call_user_func_array("foobar", array("second" => "two", "first" => "one"));

// Call the foobar() function with one named argument
call_user_func_array("foobar", array("foo", "second" => "bar"));

// Fatal error: Cannot use positional argument after named argument
call_user_func_array("foobar", array("first" => "one", "bar"));

?>

以上示例的输出类似于:

 
foobar got one and two
foobar got foo and bar

Fatal error: Uncaught Error: Cannot use positional argument after named argument

注释

注意:

在函数中注册有多个回调内容时(如使用 call_user_func()call_user_func_array()),如在前一个回调中有未捕获的异常,其后的将不再被调用。

参见

add a note

User Contributed Notes 4 notes

up
37
sebastian dot rapetti at tim dot it
2 years ago
Using PHP 8, call_user_func_array call callback function using named arguments if an array with keys is passed to $args parameter, if the array used has only values, arguments are passed positionally.

<?php

function test(string $param1, string $param2): void
{
echo
$param1.' '.$param2;
}

$args = ['hello', 'world'];
//hello world
call_user_func_array('test', $args);

$args = ['param2' => 'world', 'param1' => 'hello'];
//hello world
call_user_func_array('test', $args);

$args = ['unknown_param' => 'hello', 'param2' => 'world'];
//Fatal error: Uncaught Error: Unknown named parameter $unknown_param
call_user_func_array('test', $args);
?>
up
54
admin at torntech dot com
8 years ago
As of PHP 5.6 you can utilize argument unpacking as an alternative to call_user_func_array, and is often 3 to 4 times faster.

<?php
function foo ($a, $b) {
return
$a + $b;
}

$func = 'foo';
$values = array(1, 2);
call_user_func_array($func, $values);
//returns 3

$func(...$values);
//returns 3
?>

Benchmarks from https://gist.github.com/nikic/6390366
cufa with 0 args took 0.43453288078308
switch with 0 args took 0.24134302139282
unpack with 0 args took 0.12418699264526
cufa with 5 args took 0.73408579826355
switch with 5 args took 0.49595499038696
unpack with 5 args took 0.18640494346619
cufa with 100 args took 5.0327250957489
switch with 100 args took 5.291127204895
unpack with 100 args took 1.2362589836121
up
0
alangiderick at gmail dot com
2 months ago
It's quite interesting reading the notes in this page especially the one that mentions the difference between argument unpacking being significantly faster than calling `call_user_func_array()` directly by admin at torntech dot com.

This is true for PHP 5 but as from PHP 7.0+, there is no significant difference in the run-time between these two mechanisms of operation. The time taken is almost, if not the same for both operations, so this is already something that tells me that the PHP run-time environment has changed quite a lot (for the PHP 7 rewrite).

I used the example from admin at torntech dot com to check this hypothesis.
up
0
james at gogo dot co dot nz
18 years ago
Be aware the call_user_func_array always returns by value, as demonstrated here...

<?php
function &foo(&$a)
{
return
$a;
}

$b = 2;
$c =& call_user_func_array('foo', array(&$b));
$c++;
echo
$b . ' ' . $c;
?>

outputs "2 3", rather than the expected "3 3".

Here is a function you can use in place of call_user_func_array which returns a reference to the result of the function call.

<?php
function &ref_call_user_func_array($callable, $args)
{
if(
is_scalar($callable))
{
// $callable is the name of a function
$call = $callable;
}
else
{
if(
is_object($callable[0]))
{
// $callable is an object and a method name
$call = "\$callable[0]->{$callable[1]}";
}
else
{
// $callable is a class name and a static method
$call = "{$callable[0]}::{$callable[1]}";
}
}

// Note because the keys in $args might be strings
// we do this in a slightly round about way.
$argumentString = array();
$argumentKeys = array_keys($args);
foreach(
$argumentKeys as $argK)
{
$argumentString[] = "\$args[$argumentKeys[$argK]]";
}
$argumentString = implode($argumentString, ', ');
// Note also that eval doesn't return references, so we
// work around it in this way...
eval("\$result =& {$call}({$argumentString});");
return
$result;
}
?>
To Top