ATLChris
ATLChris

Reputation: 3296

Call a method based on the value of a property

I have a large list of methods that I call based on the value of a property. I was wondering if there is a more correct method todo what I am doing?

My code now:

if ($qvalue->fid == 1) { $this->Archive1(); }
if ($qvalue->fid == 2) { $this->Archive2(); }
if ($qvalue->fid == 3) { $this->Archive3(); }
if ($qvalue->fid == 4) { $this->Archive4(); }
if ($qvalue->fid == 5) { $this->Archive5(); }
if ($qvalue->fid == 6) { $this->Archive6(); }
if ($qvalue->fid == 7) { $this->Archive7(); }
if ($qvalue->fid == 8) { $this->Archive8(); }
if ($qvalue->fid == 9) { $this->Archive9(); }
if ($qvalue->fid == 10) { $this->Archive10(); }
...

Can I do something like this:

$this->Archive($qvalue->fid)();

I know this wont work, I just am not sure if something better is possible. Not even sure what to search to see other posts similar.

Upvotes: 0

Views: 62

Answers (1)

Rob
Rob

Reputation: 526

$method = 'Archive' . $qvalue->fid;
if (method_exists($this, $method))
{
    $this->$method();
}

Upvotes: 1

Related Questions