call_user_func_array() 调用回掉函数,并把一个数组参数坐位回掉函数。也可以执行类中的方法。
例子:
<?php
function foobar($arg, $arg2) {
echo __FUNCTION__, " got $arg and $arg2</br>";
}
class foo {
static function bar($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2</br>";
}
function food($arg, $arg2) {
echo __METHOD__, " got $arg and $arg2</br>";
}
}
call_user_func_array("foobar", array("one", "two"));
$foo = new foo;
call_user_func_array(array($foo, "food"), array("three", "four"));
//这种方式是错误的
//call_user_func_array(array("foo", "food"), array("three", "four"));
//下面这种办法,通过PHP的对象的方式调用一个静态方法,并不会报错。但是静态方法中不可以出现$this关键字
call_user_func_array(array($foo, "bar"), array("three", "four"));
//个人感觉调用静态方法还是这样会好些
call_user_func_array(array("foo", "bar"), array("three", "four"));
?>
结果:
foobar got one and two foo::food got three and four foo::bar got three and four foo::bar got three and four