Call_user_func and call_user_func_array functions: calluserfuncarray
Call_user_func (): calls a callback function to process strings,
Anonymous functions can be used, famous functions can be used, and class methods can be passed,
When using a famous function, you only need to pass the function name.
When using a class method, you must pass the class name and method name.
The first parameter passed must be the function name, anonymous function, or method.
For other parameters, you can pass one or more parameters. These parameters are automatically transferred to the callback function.
The callback function can obtain these parameters by passing parameters.
Returns the result of callback function processing.
① Pass the function name and obtain the call_user_func parameter through the callback function parameter
<? Php // first reference and then add function _ call ($ call) {// obtain the echo $ call ++ parameter passed by call_user_func by passing the parameter, '<br/> '; echo $ call ++, "<br/>" ;}// the preceding callback function has no returned value. Therefore, no returned value is returned here, _ call is the name of the above function $ re = call_user_func ('_ call', 1); // The experiment result is null, which conforms to the above conclusion var_dump ($ re );
② Passing parameters through anonymous Functions
<? Php // Add and reference call_user_func (function ($ call) {echo ++ $ call, '<br/>'; echo ++ $ call, '<br/>' ;}, 1); // The parameter passed to the anonymous function is... 1. The execution result is 2 and 3.
③ If the callback function is an anonymous function and does not have any parameters, you can obtain the parameters in other ways.
<? Php $ arg1 = 'first'; $ arg2 = 'two'; $ return = call_user_func (function () {$ arg = func_get_arg (0); // func_get_arg function: to obtain the number of parameters of a function, you must have a parameter. The parameter must be the offset of the function parameter. 0 represents the role of the first parameter $ args = func_get_args (); // func_get_args: obtain all the parameters of the function, if (func_num_args () = 1) {// function of func_num_args: obtain the number of function parameters. Note that if the function does not pass parameters, this function returns 0 return $ args [0];} else {// use | organizes the function parameters into a string return implode ('|', $ args );}}, $ arg1, $ arg2); var_dump ($ return );
④ Call · No namespace ·
<? Phpclass Func {// static method static public function _ func () {$ str = 'the class name is '. _ CLASS __. and class static method is '. _ METHOD __; if (func_num_args () {// obtain the function parameters. You can also set the parameters for the METHOD to obtain them, here, we only do not set the parameters for the form parameter to get $ arg = func_get_arg (0); return $ str. 'and argument is '. $ arg;} else {return $ str ;}// common public function _ func ($ num) {return $ num? $ Num + 1: $ num ;}/// you can use either of the following methods to pass the static class: // (1). The class name is :: method Name · var _ dump (call_user_func ("Func: _ func", 'hell World'); // input parameters here // (2, pass the array var_dump (call_user_func (array ('func', '_ func') of the Class Name and method name); // The parameter $ num = 4 is not passed here; $ o = new Func; // The passing class common method must use the object of the class · method name · $ return = call_user_func (array ($ o, '_ func'), $ num); var_dump ($ return );
⑤ Call... ·
<? Php // defines the namespace Home of the class; class Space {// static method static public function _ call ($ num) {return $ num + = 10 ;} // common method public function _ func () {return func_get_args (); // return function parameters }}// for static methods, there are two call Methods // 1. array (_ NAMESPACE __. '\ class name', 'Method name') transmits class methods, that is, array ('namespace \ class name', 'Method name ') $ return = call_user_func (array (_ NAMESPACE __. '\ Space', '_ call'), 10); // 2. you can use · _ NAMESPACE __. '\ Class Name: Method name' · transfer class method, that is, 'namespace \ Class Name: Method name' $ return1 = call_user_func ('home \ Space:: _ call', 100); var_dump ($ return); var_dump ($ return1); // call a common method without passing in a namespace, as shown in the following $ o = new Space; $ return = call_user_func (array ($ o, '_ func'), 1, 2, 3, 4, 5); var_dump ($ return );
Call_user_func function is a way for php to reference anonymous functions. Unlike JavaScript, php can assign anonymous functions to variables and reference them, But call_user_func can be used to call anonymous functions, this can also ensure that local variables are not contaminated globally. call_user_func calls callback functions not only our custom functions, but also php system functions that process strings, such as rtrim and explode, when calling these system functions, you must note that the parameters passed by call_user_func must conform to the parameter passing sequence of the system function. You can call them by yourself. For example: call the rtrim and explode functions. The following example is a practical example.
<?php$return = call_user_func('rtrim','sso;osoo;',';');$return2 = call_user_func('explode',';','sso;osoo;');var_dump($return);var_dump($return2);
Similar to the call_user_func function, there is also a call_user_func_array function, which calls and functions 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, anonymous function, or class method, and the second parameter is an array. From here, we can see that the call_user_func_array function is different from call_user_func, call_user_func_array uses the callback function to process arrays, while call_user_func uses the callback function to process strings. The fundamental difference between the two is here. You can call the call_user_func_array () function by yourself. Because their references are basically the same, there will be no more call_user_func_array statements.