The Call_user_func function is similar to a particular method of calling a function, using the following method:
Copy Code code as follows:
function A ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func (' A ', "111", "222");
Call_user_func (' A ', "333", "444");
displaying 111 222 333 444
?>
Call the internal method of the class is strange, incredibly with array, do not know how to consider the developer, of course, save the new, but also full of innovative:
Copy Code code as follows:
Class A {
Function B ($c)
{
Echo $c;
}
}
Call_user_func (Array ("A", "B"), "111");
Show 111
?>
The Call_user_func_array function is very similar to the call_user_func, except that it passes the argument in a different way, making the parameter structure clearer:
Copy Code code as follows:
function A ($b, $c)
{
Echo $b;
Echo $c;
}
Call_user_func_array (' A ', Array ("111", "222"));
Showing 111 222
?>
The Call_user_func_array function can also invoke methods within the class
Copy Code code as follows:
Class ClassA
{
function BC ($b, $c) {
$BC = $b + $c;
Echo $BC;
}
}
Call_user_func_array (Array (' ClassA ', ' BC '), Array ("111", "222"));
Show 333
?>
Both the Call_user_func function and the Call_user_func_array function support references, which tend to be more functional consistent with normal function calls:
Copy Code code as follows:
Function A (& $b)
{
$b + +;
}
$c = 0;
Call_user_func (' A ', & $c);
echo $c//Show 1
Call_user_func_array (' A ', Array (& $c));
echo $c//Show 2
Simple usage of PHP Call_user_func_array
Today in the group, there is a call to Lewis in the use of Call_user_func_array, because it has not been used before, and can not say anything, so look at the manual, found that this is written:
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.
And then there's another example:
Copy Code code as follows:
<?php
function Foobar ($arg, $arg 2) {
Echo __function__, "Got $arg and $arg 2\n";
}
class Foo {
function Bar ($arg, $arg 2) {
Echo __method__, "Got $arg and $arg 2\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 above routine is similar to the following:
Foobar got one and two
Foo::bar got three and four
Example #2 Call_user_func_array () using namespace name
Copy Code code as follows:
<?php
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 above routine is similar to the following:
Hello hannes!
Hello philip!
Example #3 Using lambda function
Copy Code code as follows:
<?php
$func = function ($arg 1, $arg 2) {
return $arg 1 * $arg 2;
};
Var_dump (Call_user_func_array ($func, Array (2, 4)); /* As of PHP 5.3.0 * *
?>
The above routines will output:
Int (8)
I'm sure it's a little clear after seeing the example.
I am so understanding this function, if the wrong, but also hope that you are not ridiculed:
The real usage of this function is somewhat similar to a function overload, because his first argument is character type, that is, the name of the function, the second argument is an array, we can think of the parameters of the function, and in fact it is so, if you read my previous article: Php pseudo overload, perhaps you can understand, It is because of the existence of this function that I find that the function overload can also be applied in this way:
Copy Code code as follows:
/**
* After the example was finished, I thought it was finished, and I met someone ask Call_user_func_array (), read the manual
* It turns out that the test function above can also be condensed into the following example,
*/
function Otest1 ($a)
{
Echo (' one parameter ');
}
function Otest2 ($a, $b)
{
Echo (' two parameters ');
}
function Otest3 ($a, $b, $c)
{
Echo (' three ");
}
function Otest ()
{
$args = Func_get_args ();
$num = Func_num_args ();
Call_user_func_array (' otest '. $num, $args);
}
Otest (1,2);
Do you see? And my original writing, in the PHP pseudo overload is mentioned in the article, just for reference ....