Briefly understand the two functions call_user_func and call_user_func_array

步履不停
Release: 2023-02-23 06:40:01
Original
1714 people have browsed it

Briefly understand the two functions call_user_func and call_user_func_array

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);
Copy after login

 ②The case of calling anonymous functions and passing parameters

';
    echo ++$call,'
'; },1);//传给匿名函数的参数为···1···,执行的结果为2,3
Copy after login

 ③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);
Copy after login

 ④Call...no namespace· ··The situation of...Class method... ·········The situation

Copy after login

The call_user_func function is a way for PHP to refer to anonymous functions. PHP, unlike js, can assign anonymous functions to Variables and references, but anonymous functions can be called through the call_user_func function, which also prevents local variables from being globally polluted. The callback function called by call_user_func is not only our custom function, but also a system function for PHP to process strings. , such as rtrim and explode. When calling these system functions, it should be noted that the parameters passed by call_user_func must conform to the parameter passing order of the system function. You can try calling them yourself. For example: call the rtrim and explode functions. I have tried the following example and it is feasible (

php video tutorial

)

Copy after login
Similar to the call_user_func function, there is also a call_user_func_array function. The call and function of this function are basically the same as the call_user_func function. , the difference is that the call_user_func_array function can only pass two parameters. The first is the callback function name, or anonymous function, or class method, and the second parameter is the array. It can also be seen from here that in fact, the call_user_func_array function is the same as The difference between call_user_func is that call_user_func_array uses a callback function to process arrays, while call_user_func uses a callback function to process strings. This is the fundamental difference between them. You can try calling the call_user_func_array() function yourself, because their references are basically the same, so I won’t write more about call_user_func_array.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!