call_user_func_array 回调函数有几种常用的回调
- 全局函数的回调
- 类的静态方法的回调
全局函数的回调
<?php
/**
* Created by PhpStorm.
* User=> binWei
*/
function fnCallBack($msg1, $msg2)
{
echo 'msg1:' . $msg1;
echo "<br />\n";
echo 'msg2:' . $msg2;
}
$params = array('hello', 'world');
call_user_func_array('fnCallBack', $params);
结果:
类的静态方法的回调
<?php
/**
* Created by PhpStorm.
* User: v_bivwei
* Date: 2019/10/15
* Time: 15:04
*/
class MyClass
{
public static function fnCallBack($msg1, $msg2)
{
echo 'msg1:' . $msg1;
echo "<br />\n";
echo 'msg2:' . $msg2;
return "is class static function";
}
}
$className = 'MyClass';
$fnName = "fnCallBack";
$params = array('class==hello', 'class=world');
$ret = call_user_func_array(array($className, $fnName), $params);
var_dump($ret);
结果:
对于类的非静态方法也可以使用这种方式。
不建议用 call_user_func_array 来回调一个对象的方法。