When you import a certain class file and want to call this class file, call_user_func_array function. Here are two examples according to different parameters:
<?php $func = function($arg1, $arg2) { return $arg1 * $arg2; }; var_dump(call_user_func_array($func, array(2, 4))); /* As of PHP 5.0 */ ?>
The output will be:
int(8) [1]
<? Class ClassA { function bc($b, $c) { $bc = $b + $c; echo $bc; } } call_user_func_array(array('ClassA','bc'), array("111", "222")); //显示 333 ?>
First parameter: class name, function. Second function: Pass in parameters
Maybe you will ask: Under what circumstances is the function call_user_func_array used? When calling a certain function, just call it directly with new. Isn’t it unnecessary?
The difference between call_user_func_array and new class names is that if the first parameter of all_user_func_array is array('class name','method name'), the system will automatically create an object for the class to execute the method directly, but it will not be executed. The __construct constructor method is equivalent to calling the static method class name::function name (parameter), and when new class name is used, the constructor will be executed first.