Home  >  Article  >  Backend Development  >  Briefly understand the two functions call_user_func and call_user_func_array

Briefly understand the two functions call_user_func and call_user_func_array

步履不停
步履不停Original
2019-06-06 16:06:531881browse

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

<?php
//先引用后增加
function _call($call){
    //通过传参获取call_user_func传过来的参数
    echo $call++,&#39;<br/>&#39;;
    echo $call++,"<br/>";
}
//上面回调函数没有返回值,所以,这里就没有返回值,_call为上面的函数的名称
$re = call_user_func(&#39;_call&#39;,1);
//实验结果为 null,符合上面的结论
var_dump($re);

 ②The case of calling anonymous functions and passing parameters

<?php
//先增加后引用
call_user_func(function($call){
    echo ++$call,&#39;<br/>&#39;;
    echo ++$call,&#39;<br/>&#39;;
},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 = &#39;first&#39;;
$arg2 = &#39;two&#39;;
$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(&#39;|&#39;,$args);
    }
},$arg1,$arg2);
var_dump($return);

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

<?php
class Func{
    //静态方法
    static public function _func(){
        $str =  &#39;THE CLASS NAME IS &#39;.__CLASS__.&#39; AND CLASS STATIC METHOD IS &#39;.__METHOD__;
        if(func_num_args()){
            //获取函数参数,获取参数也可以通过给方法设置形参来获取,这里只是没给方法设置形参获取参数的情况
            $arg = func_get_arg(0);
            return $str.&#39; and argument is &#39;.$arg;
        }else{
            return $str;
        }
    }
    //普通方法
    public function __func($num){
        return $num ? $num+1:$num;
    }
}
//传递类的静态方法,有两种方式
//(1)种,传递····类名::方法名····
var_dump(call_user_func("Func::_func",&#39;hell world&#39;));//这里传递参数
//(2)种,传递类名和方法名的数组
var_dump(call_user_func(array(&#39;Func&#39;,&#39;_func&#39;)));//这里没有传参数
$num = 4;
$o = new Func;
//传递类普通方法必须用···数组···传递···该类的对象··和···方法名···
$return = call_user_func(array($o,&#39;__func&#39;),$num);
var_dump($return);

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

)

<?php
//定义类的命名空间
namespace Home;
class Space{
    //静态方法
    static public function _call($num){
        return $num +=10;
    }
    //普通方法
    public function _func(){
        return func_get_args();//返回函数的参数
    }
}
//针对静态方法,有两种调用方式
//1.可以用array(__NAMESPACE__.&#39;\类名&#39;,&#39;方法名&#39;)传递类方法,也就是:array(&#39;命名空间\类名&#39;,&#39;方法名&#39;)
$return = call_user_func(array(__NAMESPACE__.&#39;\Space&#39;,&#39;_call&#39;),10);
//2.可以用····    __NAMESPACE__.&#39;\类名::方法名&#39;    ···传递类方法,也就是:&#39;命名空间\类名::方法名&#39;
$return1 = call_user_func(&#39;Home\Space::_call&#39;,100);
var_dump($return);
var_dump($return1);
//针对普通方法,不用传入命名空间即可调用,如下
$o = new Space;
$return = call_user_func(array($o,&#39;_func&#39;),1,2,3,4,5);
var_dump($return);
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!

Statement:
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