
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
1 2 3 4 5 6 7 8 9 10 11 | <?php
function _call( $call ){
echo $call ++,'<br/>';
echo $call ++, "<br/>" ;
}
$re = call_user_func('_call',1);
var_dump( $re );
|
Copy after login
②The case of calling anonymous functions and passing parameters
1 2 3 4 5 6 | <?php
call_user_func( function ( $call ){
echo ++ $call ,'<br/>';
echo ++ $call ,'<br/>';
},1);
|
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
1 2 3 4 5 6 7 8 9 10 11 12 13 | $arg1 = 'first';
$arg2 = 'two';
$return = call_user_func( function (){
$arg = func_get_arg(0);
$args = func_get_args();
if (func_num_args() == 1){
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | <?php
class Func{
static public function _func(){
$str = 'THE CLASS NAME IS '. __CLASS__ .' AND CLASS STATIC METHOD IS '. __METHOD__ ;
if (func_num_args()){
$arg = func_get_arg(0);
return $str .' and argument is '. $arg ;
} else {
return $str ;
}
}
public function __func( $num ){
return $num ? $num +1: $num ;
}
}
var_dump(call_user_func( "Func::_func" ,'hell world'));
var_dump(call_user_func( array ('Func','_func')));
$num = 4;
$o = new Func;
$return = call_user_func( array ( $o ,'__func'), $num );
var_dump( $return );
|
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
)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?php
namespace Home;
class Space{
static public function _call( $num ){
return $num +=10;
}
public function _func(){
return func_get_args();
}
}
$return = call_user_func( array (__NAMESPACE__.'\Space','_call'),10);
$return1 = call_user_func('Home\Space::_call',100);
var_dump( $return );
var_dump( $return1 );
$o = new Space;
$return = call_user_func( array ( $o ,'_func'),1,2,3,4,5);
var_dump( $return );
|
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!