php 函数调用的几种方式有哪些?
最常见的函数调用:
1.
function userFunction1($param1, $param2){
echo 'UserFunction1: Param1 : ',$param1,' , Param2 : ',$param2,'
';
}
userFunction1('Hello','world');
2.
$userFunction2 = function($param1, $param2){
echo 'UserFunction2: Param1 : ',$param1,' , Param2 : ',$param2,'
';
};
$userFunction2('Hello', 'PHP');
作为回调函数的函数调用:
1.
function funcWithCallback1($callback, $param1, $param2){
echo 'funcWithCallback1 : ';
if(is_callable($callback)) $callback($param1, $param2);
}
funcWithCallback1($userFunction2,'Hello','world');
2.
call_user_func
function funcWithCallback2($callback, $param1, $param2){
echo 'funcWithCallback2 : ';
if(is_callable($callback)) call_user_func($callback, $param1, $param2);
}
funcWithCallback2($userFunction2,'Hello','world');