Simple Understanding of call_user_func() and call_user_func_array() in PHP

Keywords: PHP

Original address: http://small.aiweimeng.top/index.php/archives/52.html

call_user_func: Call a parameter as a callback function

Description of usage:

call_user_func ( callable $callback [, mixed $parameter [, mixed $... ]] ) : mixed

  

Description of parameters:

The first parameter, $callback, is used as the callback function, and the other parameters are all parameters of the callback function.

$parameter: Pass in the parameter of the callback $callback function, and note that the parameter here does not refer to the pass.

The following simple examples illustrate the use of call_user_func in different situations:

//Reference first, then execute
function _call($call){
    echo $call++.'<br/>';
    echo $call++.'<br/>';
    return $call;
}
$rs = call_user_func('_call',1);
var_dump($rs);

//Result
//1
//2
//int(3)

  

First execute, then quote

$arg = 1;
call_user_func(function ($call){
    echo ++$call.'<br/>';
    echo ++$call.'<br/>';
},$arg);

//The results were 2,3

  

Callback functions do not pass values, and parameters are obtained by func_get_arg and func_get_args

$argOne = 1;
$argTwo = 2;
call_user_func(function (){
    //Get the number of parameters
    $arg = func_get_arg(0);
    var_dump($arg);
    echo '<br/>';
    //Get all the parameters and return them as arrays
    $args = func_get_args();
    var_dump($args);
    //Get the number of parameters
    $argNum = func_num_args();
    echo "<br/>";
    var_dump($argNum);
},$argOne,$argTwo);

//The result is
int(1) 
array(2) { [0]=> int(1) [1]=> int(2) } 
int(2)

  

Call class methods:

There are two forms of calling static methods in classes, and the first parameter of calling public methods can only be an array.

class Func{

    static public function _One(){
        $str = "THE CLASS NAME IS".__CLASS__." AND CLASS STATIC METHOD IS ".__METHOD__;
        $argNum = func_num_args();
        if($argNum){
            $arg = func_get_arg(0);
            return $str.' and argument is '.$arg;
        }else{
            return $str;
        }
    }


    public function _Two($num){
        return $num ? $num + 1 : $num;
    }

}

echo "<br/>";
//Calling static methods of classes
var_dump(call_user_func('Func::_One','one'));
echo '<br/>';
var_dump(call_user_func(['Func','_One']));

$num = 4;
$o = new Func;
//Calling Class Common Method
$return = call_user_func(array($o,'_Two'),$num);
echo '<br/>';
var_dump($return);

//Result:
string(79) "THE CLASS NAME ISFunc AND CLASS STATIC METHOD IS Func::_One and argument is one" 
string(59) "THE CLASS NAME ISFunc AND CLASS STATIC METHOD IS Func::_One" 
int(5)

  

The use of call_user_func when calling a namespace class is the same as above

//Calling static methods
call_user_func(array(__NAMESPACE__.'\StaticDemo','_One'),100);
call_user_func('App\StaticDemo::_One',200);
//Call the public method
call_user_func(array($obj,'_Two'),2,3,4);

  

Last:
Similar to the call_user_func function are call_user_func_array, and the function of call_user_func_array is the same as that of call_user_func.
The difference is that call_user_func handles characters with callback functions, while call_user_func_array handles arrays with callbacks, which means that the second parameter of call_user_func_array can only be an array.

Posted by yujikaido on Thu, 14 Mar 2019 20:48:25 -0700