一:方法解释
call_user_func:把第一个参数作为回调函数进行调用,其余参数作为回调函数的参数
call_user_func_array:把第一个参数作为回调函数进行调用,第二个参数传入数组,将数组中的值作为回调函数的参数
二:call_user_func和call_user_func_array简单介绍
1:第一个参数传入方法名
例:
(1)原生php中使用:
function test($test1, $test2) {
return $test1 . $test2;
}
echo call_user_func('test', 'a','b');//输出结果为ab
echo call_user_func_array('test', ['c', 'd']);//输出结果为cd
(2)框架中使用:
public function test($test1,$test2)
{
return $test1 . $test2;
}
echo call_user_func(array($this, 'test'), 'a', 'b');//输出结果为ab
echo call_user_func_array(array($this, 'test'), ['a', 'b']);//输出结果为cd
2:第一个参数作为匿名函数
原生和框架中使用方式相同:
echo call_user_func(function ($test){return $test;}, 1);//输出结果为1
echo call_user_func_array(function ($test){return $test;}, [1]);//输出结果为1
3:第一个参数调用类中的方法
class Test {
static function test1($test){
return $test;
}
}
(1):原生php调用:
echo call_user_func(array('test', 'test1'), 1);//输出结果为1
echo call_user_func_array(array('test', 'test1'), [1]);//输出结果为1
(2):框架中调用:
echo call_user_func(array(new Test(), 'test'), 1);//输出结果为1
echo call_user_func_array(array(new Test(), 'test'), [1]);//输出结果为1
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。