Simon
Simon

Reputation: 5698

Dynamic Function Call PHP

I have a very specific question: is the following code possible in one line? Or is there a beter way to do the same?

$key = rand(1,100);

$temp = 'GetObjects'.ucfirst($key).'Array';
$objects = $this->module->$temp();

Like this:

$objects = $this->module->'GetObjects'.ucfirst($key).'Array'();

Upvotes: 0

Views: 281

Answers (1)

Tyler Carter
Tyler Carter

Reputation: 61567

Maybe

$objects = $this->module->{'GetObjects'.ucfirst($key).'Array'}();

or

$objects = call_user_func(array($this->module, 'GetObjects'.ucfirst($key).'Array'));

Upvotes: 4

Related Questions