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.
Mutable functions cannot be used for examples such as ECHO, Print, unset (), Isset (), empty (), include, require, and similar language constructs. These structures need to be used as mutable functions using their own wrapper functions.
Example #1 Variable Function example
<?phpfunction foo () { echo "in foo () <br/>\n";} function Bar ($arg = " ) { echo" in bar (); argument is ' $arg '. <br/>\n ";} Use Echo's wrapper 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 syntax of a mutable function to invoke a method of an object.
Example #2 Variable Method Example
<?phpclass 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 ()?>
When a static method is called, the function call takes precedence over the static property:
Example #3 Variable methods and static properties examples
<?phpclass foo{ static $variable = ' static property '; static function Variable () { echo ' Method Variable called '; }} Echo Foo:: $variable; This prints ' static property '. It does need a $variable in this scope. $variable = "variable"; Foo:: $variable (); This calls $foo->variable () reading $variable the This scope. >