Home  >  Article  >  Backend Development  >  call_user_func_array call instance

call_user_func_array call instance

巴扎黑
巴扎黑Original
2016-11-23 14:09:011292browse

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(&#39;ClassA&#39;,&#39;bc&#39;), 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.


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
Previous article:php object interfaceNext article:php object interface