[PHP] Variable function-callback

Variable function

First of all, the definition of a variable function is:
there is a value saved by a variable $a, which happens to be the name of a function, then when using the function, you can change the wording: variable a().

//定义函数
function display(){
    
    
	return __FUNCTION__;
}
//定义变量
$a = 'display';
//使用变量可以这样写:
$a();

So, where are variable functions generally used?

It is more commonly used when doing some projects in the future: For example, when many system functions are used, some external user-defined functions need to be used inside the system functions.

For example, there is a user-defined function for finding the fourth power of a number:

function user_function($num){
    
    
	return $num*$num*$num*$num;
}

But for example, when someone wants to call your API, he doesn't know what function you have (because there are too many), he only knows what is in the system function. This requires putting this fourth power function into the system function, and when using it, you need to use a variable function.

If this system function is like this:

function sys_function($arg1,$arg2){
    
    
	return $arg1($arg2);
}

Then, for example, to find the 4th power of 10, you can write:

sys_function('user_function',10);

Sometimes, it is necessary to perform some processing on the data submitted by the user. At this time, it can be changed in the system function, such as:

function sys_function($arg1,$arg2){
    
    
	$arg2 += 10;
	return $arg1($arg2);

There are many similar situations, such as adding some data, etc., which can be used in this way.

The above is the callback process.

Guess you like

Origin blog.csdn.net/qq_44899247/article/details/105954267