call_user_func_array call instance

巴扎黑
Release: 2016-11-23 14:09:01
Original
1291 people have browsed it

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 */    
?>
Copy after login

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    
?>
Copy after login


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.


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!