Not exactly sure why but this has a certain code smell in my opinion. But anyway...
Method a): Implement the "magic" method __call($name, $params).
<?php
class Foo {
public function abc_function1() {
echo "function #1";
}
public function abc_function2() {
echo "function #2";
}
public function abc_function3() {
echo "function #3";
}
public function __call($name, $params) {
$fqn = 'abc_'.$name;
if ( method_exists($this, $fqn) ) {
call_user_func_array( array($this, $fqn), $params);
}
}
}
$f = new Foo;
$f->function2();
Method b): Same idea, just without the automagical mapping.
<?php
class Foo {
public function abc_function1() {
echo "function #1";
}
public function abc_function2() {
echo "function #2";
}
public function abc_function3() {
echo "function #3";
}
public function doSomething($x, $y, $z) {
$fqn = 'abc_'.$x;
if ( method_exists($this, $fqn) ) {
call_user_func_array( array($this, $fqn), array($y, $z));
}
}
}
$f = new Foo;
$f->doSomething('function2', 1, 2);
Method c) If you know the number of parameter you can also use
$this->$fqn($,y, $z)
instead of
call_user_func_array( (array($this, $fqn), array($y, $z) );
see also: http://docs.php.net/call_user_func_array and http://docs.php.net/functions.variable-functions