Reputation: 353
I was wondering if there is such a thing like dynamic Class and Function calling possible?
For example, I have these two operations:
$this->foo->value();
$this->bar->value();
and I would like to call it somewhat like this:
$this->($var)->value();
right now I'm doing it this way (but would love the above methode)
if($var == 'foo')
$this->foo->value();
else
$this->bar->value();
Upvotes: 2
Views: 58
Reputation: 32232
You almost had it.
$var = 'foo';
$this->$var->value();
Is equivalent to
$this->foo->value():
Similarly:
$var = 'bar';
$this->foo->$var();
Is equivalent to;
$this->foo->bar();
Upvotes: 1