Jump to content

Archived

This topic is now archived and is closed to further replies.

ghostz00

Using a variable in a function call

Recommended Posts

I want to call a function using a variable.

example
[code]
class example{
$var=100;
$variable=100;
}

$func="var"; // or variable

$ex=new example;

//this is where I need help somehow I need to use the $func variable to call the right variable.
echo $ex->($func);
[/code]

Share this post


Link to post
Share on other sites
Do you mean this

[code]class example{
      var $var=100;
      var $variable=120;
}

$func1="var";
$func2="variable";

$ex=new example;

echo $ex->$func1;  // --> 100
echo $ex->$func2;  // --> 120[/code]

or this?

[code]class example{
      var $var=100;
      var $variable=120;

      function f1() {return $this->var;}
      function f2() {return $this->variable;}
}

$func1="f1";
$func2="f2";

$ex=new example;

echo $ex->$func1();  // --> 100
echo $ex->$func2();  // --> 120[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.