PHP supports the concept of mutable functions. This means that if a variable name has parentheses after it, 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 to implement some purposes, including callback functions, function tables, and so on.
Variable functions cannot be used for language structures such as echo (), print (), unset (), Isset (), empty (), include (), require (), and similar statements. These structures need to be used as variable functions using their own wrapper functions.
Example #1 Variable Function example
Copy CodeThe code is as follows:
function foo () {
echo "in Foo ()
/n ";
}
function bar ($arg = ") {
echo "in Bar (); Argument was ' $arg '.
/n ";
}
Use Echo's wrapper function
function Echoit ($string)
{
Echo $string;
}
$func = ' Foo ';
$func (); This calls Foo ()
$func = ' Bar ';
$func (' Test '); This calls bar ()
$func = ' Echoit ';
$func (' Test '); This calls Echoit ()
?>
You can also use the properties of a mutable function to invoke a method of an object.
Example #2 Variable Method Example
Copy CodeThe code is as follows:
Class Foo
{
function Variable ()
{
$name = ' Bar ';
$this $name (); This calls the Bar () method
}
function Bar ()
{
echo "This is Bar";
}
}
$foo = new Foo ();
$funcname = "Variable";
$foo $funcname (); This calls $foo->variable ()
?>
http://www.bkjia.com/PHPjc/327615.html www.bkjia.com true http://www.bkjia.com/PHPjc/327615.html techarticle PHP supports the concept of mutable functions. This means that if a variable name has parentheses after it, 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 to implement ...