Could I run a function from a variable? Say I had $funcVar = somefunction
, would I be able to run it like this; $funcVar()
? Or would I have to have the variable set to; $funcVar = somefunction()
, and just have this; $funcVar
in my code somewhere?
You Can store the name of a function in an variable and then call the function using variable
Example:
function example(){
// Your Code
}
$fun="example";
//Calling function
$fun();
Note: This is not something like function example()
is refereed/stored in variable $fun
its just because of the value of variable is same as function name ,any changes in value of variable and you wont be able to call the function using this variable.
Update: Using Class Example:
class test{
static function demo(){
echo "hai
";
}
}
$a="demo";
test::$a();
$ob=new test();
$ob->$a();
You can assign a function to a variable. E.g, assume you have the function myFunc:
function myFunc() {
echo "hello, world!";
}
It can be assigned to a variable
$var = "myFunc";
And then called:
$var();