PHP supports the concept of variable functions. This means that if a variable name has parentheses, PHP will look for a function with the same name as the value of the variable and try to execute it. Variable functions can be used for some purposes, including callback functions and function tables.
Variable functions cannot be used in language structures, such as echo (), print (), unset (), isset (), empty (), include (), require (), and similar statements. You need to use your own packaging functions to use these structures as variable functions.
Example #1 Variable Function Example
Copy codeThe Code is as follows:
<? Php
Function foo (){
Echo "In foo () <br/>/n ";
}
Function bar ($ arg = ''){
Echo "In bar (); argument was '$ arg'. <br/>/n ";
}
// Use echo's packaging function
Function echoit ($ string)
{
Echo $ string;
}
$ Func = 'foo ';
$ Func (); // This callfoo ()
$ Func = 'bar ';
$ Func ('test'); // This callbar ()
$ Func = 'echoit ';
$ Func ('test'); // This callechoit ()
?>
You can also call an object method by using the features of a variable function.
Example #2 variable method Example
Copy codeThe Code is as follows:
<? Php
Class Foo
{
Function Variable ()
{
$ Name = 'bar ';
$ This-> $ name (); // This callthe Bar () method
}
Function Bar ()
{
Echo "This is Bar ";
}
}
$ Foo = new Foo ();
$ Funcname = "Variable ";
$ Foo-> $ funcname (); // This CILS $ foo-> Variable ()
?>