call_user_func(): Call a callback function to process a string,
You can use anonymous functions, you can use named functions, you can pass class methods,
When using a named function, you only need to pass the name of the function
When using a class method, you need to pass the class name and method name
The first parameter passed must be a function name, or an anonymous function, or a method
For other parameters, you can pass one parameter or multiple parameters, and these parameters will be automatically passed to the callback function
And the callback function can obtain these parameters by passing parameters
Return the result processed by the callback function
① Pass the function name and obtain the parameters passed by call_user_func through the formal parameters of the callback function
'; echo $call++,"
"; } //上面回调函数没有返回值,所以,这里就没有返回值,_call为上面的函数的名称 $re = call_user_func('_call',1); //实验结果为 null,符合上面的结论 var_dump($re);
②The case of calling anonymous functions and passing parameters
'; echo ++$call,'
'; },1);//传给匿名函数的参数为···1···,执行的结果为2,3
③The callback function is an anonymous function, and the anonymous function has no parameters and the parameters are obtained through other methods
$arg1 = 'first'; $arg2 = 'two'; $return = call_user_func(function(){ $arg = func_get_arg(0); //func_get_arg函数作用:获取函数的第几个参数,必须要有参数,参数必须为函数参数的偏移量,0代表第一个参数 $args = func_get_args();//func_get_args的作用:获取函数所有的参数 if(func_num_args() == 1){//func_num_args函数的作用:获取函数参数的个数,注意,假如函数没有传参,该函数返回0 return $args[0]; }else{ //用|把函数的参数组织成字符串 return implode('|',$args); } },$arg1,$arg2); var_dump($return);
④Call...no namespace· ··The situation of...Class method... ·········The situation
Copy after login
php video tutorial
)Copy after login
The above is the detailed content of Briefly understand the two functions call_user_func and call_user_func_array. For more information, please follow other related articles on the PHP Chinese website!