Home  >  Article  >  Backend Development  >  PHP function call_user_func and call_user_func_array_PHP tutorial

PHP function call_user_func and call_user_func_array_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:02:47891browse

php tutorial function call_user_func and call_user_func_array
The call_user_func function is similar to a special method of calling a function. The method of use 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
?>

It is strange to call the method inside the class. It actually uses array. I don’t know what the developer thought about it. Of course, new is omitted, which is also full of novelty:
class a {
function b($c)
{
echo $c;
}
}
call_user_func(array("a", "b"),"111");
//Display 111
?>

The call_user_func_array function is very similar to call_user_func, except that the parameters are passed in a different way to make the parameter structure clearer:
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 methods within the class

Class ClassA
{

function bc($b, $c) {
$bc = $b + $c;
echo $bc;
}
}
call_user_func_array(array('ClassA','bc'), array("111", "222"));

//Display 333
?>

Both the call_user_func function and the call_user_func_array function support references, which makes them more functionally consistent with ordinary function calls:
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 person named Lewis asked about the usage of call_user_func_array. Since I had never used it before, I couldn’t say anything, so I 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 parameters
Description
mixed call_user_func_array ( callback function, array param_arr )


Call a user defined function given by function, with the parameters in param_arr.
Then there is another example:

I believe you should understand a little bit after reading the examples, right?


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445336.htmlTechArticlephp tutorial function call_user_func and call_user_func_array The call_user_func function is similar to a special method of calling a function. The method of use is as follows: function a($b,$c) { echo $b; echo $c;...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn