PHP supports the concept of variable functions. This means that if a variable name has parentheses
The system looks for a function with the same name as the variable value and tries 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
<? 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 calls echoit()
?>
You can also call an object method by using the features of a variable function.
Example #2 variable method example
<?php
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()
?>