Ucenter source code has a function Call_user_func, beginning to think it is a function of their own definition, the results are nowhere to be found. It was later learned that Call_user_func is a built-in function of PHP that allows the user to invoke a directly written function and pass in a certain parameter, summarizing the use of the function below.
The Call_user_func function is similar to a particular method of calling a function, using the following method:
<?php
function Nowamagic ($a, $b)
{
echo $a;
echo $b;
}
Call_user_func (' Nowamagic ', "", "");
Call_user_func (' Nowamagic ', "", "");
Show
?>
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, also quite innovative:
<?php
Class A {
function B ($c)
{
echo $c;
}
}
Call_user_func (Array ("A", "B"), "");
Show
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:
<?php
function A ($b, $c)
{
echo $b;
echo $c;
}
Call_user_func_array (' A ', Array ("", ""));
Show
The Call_user_func_array function can also invoke methods within the class:
<?php
Class classa
{
function BC ($b, $c) {
$BC = $b + $c;
echo $bc;
}
}
Call_user_func_array (Array (' ClassA ', ' BC '), Array ("", ""));
Show
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:
<?php
function A ($b)
{
$b + +;
}
$c =;
Call_user_func (' A ', $c);
echo $c//Show
Call_user_func_array (' A ', Array ($c));
echo $c//Display
?>
In addition, both call_user_func functions and Call_user_func_array functions support references.
View Sourceprint?
<?php
function Increment (& $var)
{
$var + +;
}
$a =;
Call_user_func (' increment ', $a);
echo $a;
call_user_func_array (' Increment ', array (& $a));//can use this instead
echo $a;//
?>
Here's an introduction to using Call_user_func to invoke custom functions
Using the Call_user_func function, you can call custom functions by passing in string functions and support references.
1.mixed Call_user_func (Callable $callback [, Mixed $parameter [, mixed $ ...]])
Invokes the custom function provided by the first argument, followed by the parameters of the custom function, and returns the result of the custom function
function say ($word)
{
echo $word;
}
Call_user_func (' say ', ' Hello World '); Hello World
Of course you can also call methods in a class:
class A {
function say ($word = ')
{
echo $word;
}
}
$a = new A (); Note that it must be instantiated unless it is a static
Call_user_func (Array ($a, ' say '), ' Hello World ');//hello world
2.mixed Call_user_func_array (callable $callback, array $param _arr)
In other words call_user_func_array and call_user_func function functions, just call the parameters a little different:
function A ($word)
{
echo $word;
}
Class A {
function say ($word = ')
{
echo $word;
}
}
Call_user_func_array (' A ', array (' Hello World ')); Hello world
$a = new A ();/Note that must be instantiated unless it is a static
Call_user_func_array (Array ($a, ' say '), array (' Hello World)); Hello World
Note: The other similar two functions are Call_user_method and Call_user_method_array (), but they are deactivated after PHP4.1.