一直以为call_user_func_array
函数只是单纯的利用自定义的方法回调处理数组,知道今天才发现还能调用类中的函数。用法如下:
class Test
{
static public function demo($a, $b)
{
echo $a + $b;
}
public function show($a, $b)
{
echo $a + $b;
}
}
// 调用类中的静态方法
// 类名方法名以数据形式
call_user_func(['Test', 'demo'], 1, 2); // 3
call_user_func_array(['Test', 'demo'], [1, 2]); // 3
// 类名方法名以字符串形式
call_user_func('Test::demo', 1, 2); // 3
call_user_func_array('Test::demo', [1, 2]); // 3
// 调用类中的动态方法,对象和方法必须通过数组形式传递
call_user_func([new Test, 'show'], 1, 2); // 3
call_user_func_array([new Test, 'show'], [1, 2]); // 3
这个不错耶,我喜欢!