php8: 用call_user_func_array调用回调函数
一,代码:
<?php
//调用函数一
function afunc($b, $c) {
echo "afunc:参数b:".$b.":<br/>";
echo "afunc:参数c:".$c.":<br/>";
}
//调用函数二
function bfunc($b, $c) {
$sum = $b+$c;
return $sum;
}
call_user_func_array('afunc', array("老王", "张三"));
$sum = call_user_func_array('bfunc', array(11, 33));
echo "bfunc:sum:".$sum.":<br/>";
//调用类的静态方法和普通方法
Class ClassA {
static function methoda($b, $c) {
$bc = $b + $c;
echo $bc;
return $bc;
}
function methodb($b, $c) {
$bc = $b + $c;
echo $bc;
return $bc;
}
}
$sum = call_user_func_array(array('ClassA','methoda'), array(111, 222));
echo "ClassA methoda:".$sum.":<br/>";
$obj = new ClassA;
$sum = call_user_func_array(array($obj, "methodb"), array(11,33));
echo "ClassA methodb:".$sum.":<br/>";
?>
二,测试效果:
afunc:参数b:老王:
afunc:参数c:张三:
bfunc:sum:44:
333ClassA methoda:333:
44ClassA methodb:44: