call_user_func_array

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

call_user_func_array���ûص�����������һ�����������Ϊ�ص������IJ���

˵��

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

�ѵ�һ��������Ϊ�ص�������callback�����ã��Ѳ�����������param_arr��Ϊ�ص������ĵIJ������롣

����

callback

�����õĻص�������

param_arr

Ҫ������ص����������飬�����������������顣

����ֵ

���ػص������Ľ�����������Ļ��ͷ���FALSE

������־

�汾 ˵��
5.3.0 �������������Ĺؼ��ֵĽ���������ǿ���ڴ�֮ǰ��ʹ������ð��������һ����������һ��������������Ϊ��������Ϊ�ص������Ļ������ᷢ��һ��E_STRICT�ľ��棬��Ϊ�������IJ�������Ϊ��̬������

����

Example #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

Example #2 call_user_func_array()ʹ�������ռ�����

<?php

namespace Foobar;

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

// As of PHP 5.3.0
call_user_func_array(__NAMESPACE__ .'\Foo::test', array('Hannes'));

// As of PHP 5.3.0
call_user_func_array(array(__NAMESPACE__ .'\Foo''test'), array('Philip'));

?>

�������̵���������ڣ�

Hello Hannes!
Hello Philip!

Example #3 �������ĺ�����Ϊ�ص�����call_user_func_array()

<?php

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

var_dump(call_user_func_array($func, array(24))); /* As of PHP 5.3.0 */

?>

�������̻������

int(8)

Example #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

ע��

Note:

PHP 5.4֮ǰ�����param_arr����IJ��������ô�ֵ����ô����ԭ����Ĭ�ϵĸ��������Dz������ô�ֵ�����������÷�ʽ���뵽�ص���������Ȼ�����ô�ֵ���ַ�ʽ�����ݲ������ص����������ᷢ����֧�ֵľ��棬���Dz�����ô˵�����������Dz���֧�ֵġ�������PHP 5.4���汻ȥ���ˡ����ң���Ҳ���������ڲ�������for which the function signature is honored������ص�����Ĭ��������Ҫ���ܵIJ��������ô��ݵ�ʱ�򣬰�ֵ���ݣ�����������һ�����档call_user_func() ���᷵�� FALSE��there is, however, an exception for passed values with reference count = 1, such as in literals, as these can be turned into references without ill effects — but also without writes to that value having any effect —; do not rely in this behavior, though, as the reference count is an implementation detail and the soundness of this behavior is questionable����

Note:

�ں�����ע���ж���ص�����ʱ(��ʹ�� call_user_func() �� call_user_func_array())������ǰһ���ص�����δ������쳣�����Ľ����ٱ����á�

�μ�