Understanding of PHP callback functions call_user_func and call_user_func_array()

Keywords: PHP

call_user_func(function,param); // The first parameter is the function name of the callback function, and the second parameter is the parameter.

  

The call_user_func function is similar to a special method of calling functions. Its main types are as follows:

1. Call a named function

2. Calling anonymous functions

2. Method of calling class

 

1. There are two kinds of incoming named functions:

A. Input is a self-defined function

<?php
function hasName($a,$b)   
{   
    echo $a;   
    echo $b;   
}   
call_user_func('hasName', "111","222");   // The first parameter is the name of the function, and the later test callback function returns 111 222.
call_user_func('hasName', "333","444");   // Return 333 444
?>

 

b. The PHP system functions are passed in: when calling these system functions, the parameters passed by call_user_func must conform to the order in which the system functions are transferred.

1 <?php
2 $return = call_user_func('rtrim','sso;osoo;',';');
3 $return2 = call_user_func('explode',';','sso;osoo;');
4 var_dump($return); 
5 var_dump($return2); 

 

2. Calling anonymous functions: Calling anonymous functions can also be divided into anonymous functions and non-anonymous functions.

a. Reference transmission

<?php
//Add first and quote later
call_user_func(function($call){
    echo ++$call,'<br/>';
    echo ++$call,'<br/>';
},1);//The parameters passed to the anonymous function are···1···,The result of execution is 2.,3

b. No reference

<?php
$param1 = 'first';
$param2= 'two';
$return = call_user_func(function(){
    $arg = func_get_arg(0); //func_get_arg Function function: To obtain the number of parameters of a function, there must be parameters. The parameters must be the offset of the parameters of the function, and 0 represents the first parameter.
    $args = func_get_args();//func_get_args Function: Get all the parameters of a function
    if(func_num_args() == 1){//func_num_args Function: Get the number of parameters of the function. Note that if the function has no parameters, the function returns 0.
        return $args[0];
    }else{
        //use|Organize the parameters of a function into strings
        return implode('|',$args);
    }
},$param1 ,$param2);
var_dump($return);

 

 

3. The methods of calling classes are divided into calling methods with namespaces and calling methods without namespaces, and classes are divided into static methods and common methods.

  • Calling namespace methods
//Namespaces defining classes
namespace Home;
class Space{
    //Static method
    static public function _call($num){
        return $num +=10;
    }
    //Common method
    public function _func(){
        return func_get_args();//The parameters of the return function
    }
}

//There are two ways to call static methods

//1. Class methods can be passed by array (NAMESPACE. 'class name','method name'), that is, array('namespace class name','method name').
$return = call_user_func(array(__NAMESPACE__.'\Space','_call'),10);
//2.Can use····    __NAMESPACE__.'\Class name::Method name'    ···Transfer class methods, that is:'Namespace\Class name::Method name'
$return1 = call_user_func('Home\Space::_call',100);
var_dump($return);
var_dump($return1);
//For normal methods, you can call them without passing in a namespace, as follows $o = new Space; $return = call_user_func(array($o,'_func'),1,2,3,4,5); var_dump($return);

 

  • Calling methods without namespaces
<?php
class Func{
    //Static method
    static public function _func(){
        $str =  'THE CLASS NAME IS '.__CLASS__.' AND CLASS STATIC METHOD IS '.__METHOD__;
        if(func_num_args()){
            //Get function parameters, get parameters can also be obtained by setting parameters to the method, here is only the case of not setting parameters to the method to get parameters.
            $arg = func_get_arg(0);
            return $str.' and argument is '.$arg;
        }else{
            return $str;
        }
    }
    //Common method
    public function __func($num){
        return $num ? $num+1:$num;
    }
}
//There are two ways to pass static methods of classes
//(1) Species, Transfer... Class Name: Method Name····
var_dump(call_user_func("Func::_func",'hell world'));//Pass parameters here
//(2) Species, arrays passing class names and method names
var_dump(call_user_func(array('Func','_func')));//There are no parameters passed here.
$num = 4;
$o = new Func;
//Common methods of passing classes must be used···array···transmit···Objects of this class··and···Method name··· $return = call_user_func(array($o,'__func'),$num); var_dump($return);

 

Call_user_func_array() is similar to call_user_func_array(), except 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 is the array.

Posted by aarbrouk on Mon, 18 Mar 2019 19:45:27 -0700