call_user_func_array传参问题

WBOY
Release: 2016-06-23 13:57:33
Original
1141 people have browsed it

<?phpfunction a() {    $p['asdf'] = '123';    $p['dfd'] = 234;    $p['hj3'] = 'fdg';    call_user_func_array('b', $p);}function b($p) {    $p2 = func_get_args();    var_dump($p2);    var_dump($p);}a();?>
Copy after login

会输出下面两行:
array(3) { [0]=> string(3) "123" [1]=> int(234) [2]=> string(3) "fdg" }
string(3) "123"

为什么输出$p时不是整个数组,而只是string(3) "123"?我看手册是这么写的:
mixed call_user_func_array ( callable $callback , array $param_arr )
param_arr:The parameters to be passed to the callback, as an indexed array. 
就是说会传递关联数组。


回复讨论(解决方案)

对于你的代码
call_user_func_array('b', $p);
实际执行的是
b('123', 123, 'fdg')
即向函数 b 传递了 3 个参数

而 function b($p) 只声明了一个形参 $p
所以在函数中可以直接访问到他 var_dump($p);//123

刚刚看了其他例子,明白了。感谢老徐

function a() {
    $p['asdf'] = '123';
    $p['dfd'] = 234;
    $p['hj3'] = 'fdg';
    call_user_func_array('b', array($p));
}
 
function b($p) {
    $p2 = func_get_args();
    var_dump($p2);
    var_dump($p);
}
 
a();
?>

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template