Home > Backend Development > PHP Tutorial > Analysis of call_user_func function and call_user_func_array function_PHP tutorial

Analysis of call_user_func function and call_user_func_array function_PHP tutorial

WBOY
Release: 2016-07-13 10:31:19
Original
1288 people have browsed it

Today by chance, I saw the call_user_func_array() function in PHP again in the official documentation of CodeIgniter. I also encountered it in the code of the discuz forum last year. I checked this function online today. Usage, let's share with you the usage of the call_user_func_array and call_user_func functions, and also provide usage instructions for the func_get_args() function and func_num_args() function.

The call_user_func function is used when a function needs to be called dynamically. This function has two uses, which are explained below:

(1) Call the lonely function:

<?php
function funa($b,$c){
	echo $b;
	echo $c;
}
call_user_func('funa',"111","222");
call_user_func('funa',"333","444");
//结果输出:111 222 333 444
//大家有没有发现,这个用法有点像javascript中的call方法
?>
Copy after login

(2) Call the function inside the class:

<?php
class a{
	function b(){
		$args=func_get_args();
		$num=func_num_args();
		print_r($args);
		echo $num;
	}
}
call_user_func(array("a","b"),"111","222");
?>
Copy after login

For the above example, the output is as follows:

Array
(
[0] => 111
[1] => 222
)
2

Tip: The func_get_args() function gets the parameters passed into the function and returns an array. The func_num_args() function gets the number of parameters passed into the function.

Let’s take a look at call_user_func_array. This function is also used when a function needs to be called dynamically. Its usage is similar to the call_user_func function, except that the parameter is passed in an array. The example is as follows:

<?php
function a($b,$c){
	echo $b;echo $c;
}
call_user_func_array('a',array("111","222"));
//结果输出:111 222
?>
Copy after login

The call_user_func_array function can also call methods inside the class, such as:

<?php
Class ClassA{
	function bc($b,$c){
		$bc=$b + $c;echo $bc;
	}
}
call_user_func_array(array('ClassA','bc'),array("111","222"));
//结果输出:333
?>
Copy after login

Let’s look at another example of dynamically calling a function:

<?php
function otest1 ($a){
	echo( '一个参数' );
}
function otest2 ( $a,$b){
	echo( '二个参数' );
}
function otest3 ( $a ,$b,$c){
	echo( '三个啦' );
}
function otest (){
	$args=func_get_args();
	$num=func_num_args();
	call_user_func_array( 'otest'.$num,$args  );
}
otest("11");
otest("11","22");
otest("11","22","33");
?>
Copy after login

Readers can run the above examples themselves and the impression will be even more profound.

Articles you may be interested in

  • PHP filter_var() function Filter function
  • Using php functions in smarty templates and how to use a smarty template Variables use multiple functions
  • php prompts Call to undefined function curl_init() Error solution
  • Summary of JavaScript array operation functions (push, pop, join, shift, unshift, slice, splice, concat)
  • PHP uses filter function to verify email, url and ip address
  • PHP generates continuous numeric (letter) array function range() analysis, PHP lottery program function
  • Use PHP function memory_get_usage to obtain the current PHP memory consumption to optimize program performance
  • Fatal error Call to undefined function date_default_timezone_set()

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764114.htmlTechArticleToday by chance, I saw the call_user_func_array() function in php again in the official documentation of CodeIgniter. Last year I have also encountered it in the code of the discuz forum. Today on the Internet...
Related labels:
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