When I look at the UCenter, there is a function call_user_func, which is puzzling, because I thought it was a self-defined function and the results were not found everywhere, later, Baidu learned that call_user_func is a built-in function called call_user_func, which is similar to a special method to call a function. the usage is as follows:
The code is as follows:
Function a ($ B, $ c)
{
Echo $ B;
Echo $ c;
}
Call_user_func ('a, "111", "222 ");
Call_user_func ('a, "333", "444 ");
// Display 111 222 333 444
?>
The internal method of calling the class is strange. the method actually uses an array and does not know how developers think about it. of course, the new method is saved, which is also innovative:
The code is as follows:
Class {
Function B ($ c)
{
Echo $ c;
}
}
Call_user_func (array ("a", "B"), "111 ");
// Display 111
?>
The call_user_func_array function is similar to call_user_func, but the parameter is passed in another way to make the parameter structure clearer:
The code is as follows:
Function a ($ B, $ c)
{
Echo $ B;
Echo $ c;
}
Call_user_func_array ('A', array ("111", "222 "));
// Display 111 222
?>
The call_user_func_array function can also call internal methods of the class.
The code is as follows:
Class ClassA
{
Function bc ($ B, $ c ){
$ Bc = $ B + $ c;
Echo $ bc;
}
}
Call_user_func_array (array ('classa ', 'BC'), array ("111", "222 "));
// Display 333
?>
Call_user_func and call_user_func_array functions support reference, which makes them more functional than normal function calls:
The code is as follows:
Function a (& $ B)
{
$ B ++;
}
$ C = 0;
Call_user_func ('A', & $ c );
Echo $ c; // Display 1
Call_user_func_array ('A', array (& $ c ));
Echo $ c; // Display 2
Simple usage of call_user_func_array in php
Today, in the group, a user named lewis asked call_user_func_array for usage. because he had never used it before and could not say anything, he looked at the manual and found that it was written like this:
Call_user_func_array
(PHP 4> = 4.0.4, PHP 5)
Call_user_func_array -- Call a user function given with an array of parametersDescription
Mixed call_user_func_array (callback function, array param_arr)
Call a user defined function given by function, with the parameters in param_arr.
There is another example:
The code is as follows:
Function foobar ($ arg, $ arg2 ){
Echo _ FUNCTION __, "got $ arg and $ arg2 \ n ";
}
Class foo {
Function bar ($ arg, $ arg2 ){
Echo _ METHOD __, "got $ arg and $ arg2 \ n ";
}
}
// Call the foobar () function with 2 arguments
Call_user_func_array ("foobar", array ("one", "two "));
// Call the $ foo-> bar () method with 2 arguments
$ Foo = new foo;
Call_user_func_array (array ($ foo, "bar"), array ("three", "four "));
?>
The output of the preceding routine is similar:
Foobar got one and two
Foo: bar got three and four
Example #2 call_user_func_array () using namespace name
The code is as follows:
Namespace Foobar;
Class Foo {
Static public function test ($ name ){
Print "Hello {$ name }! \ N ";
}
}
// As of PHP 5.3.0
Call_user_func_array (_ NAMESPACE _. '\ Foo: test', array ('hannes '));
// As of PHP 5.3.0
Call_user_func_array (array (_ NAMESPACE _. '\ Foo', 'test'), array ('Philip '));
?>
The output of the preceding routine is similar:
Hello Hannes!
Hello Philip!
Example #3 Using lambda function
The code is as follows:
$ Func = function ($ arg1, $ arg2 ){
Return $ arg1 * $ arg2;
};
Var_dump (call_user_func_array ($ func, array (2, 4);/* As of PHP 5.3.0 */
?>
The above routine will output:
Int (8)
I believe I should understand the example after reading it?
I understand this function in this way. if not, I hope you will not laugh at it:
The actual usage of this function is a bit similar to the function overload, because its first parameter is of the struct type, that is, the function name, and the second parameter is an array, we can use this function as a parameter. In fact, this is the case. if you have read my previous article: PHP pseudo-reload, you may be able to understand it, because of the existence of this function, I found that function overloading can also be used as follows:
The code is as follows:
/**
* After the example is completed, I thought it was finished. someone asked call_user_func_array () and read the manual.
* In the past, the test function above can be simplified into the following example,
*/
Function otest1 ($)
{
Echo ('one parameter ');
}
Function otest2 ($ a, $ B)
{
Echo ('Two parameters ');
}
Function otest3 ($ a, $ B, $ c)
{
Echo ('three loan ');
}
Function otest ()
{
$ Args = func_get_args ();
$ Num = func_num_args ();
Call_user_func_array ('otest'. $ num, $ args );
}
Otest (1, 2 );
See no? My initial writing method was mentioned in the article "PHP pseudo-heavy load" for reference only ....