One option is to use php's call_user_func_array()
;
Example:
call_user_func_array(array($this, 'to' . ucfirst($this->format)), array());
You can also use the self
keyword or the class name with the scope resolution operator.
Example:
$name = 'to' . ucfirst($this->format);
self::$name();
className::$name();
However, what you have posted using php's variable functions is also completely valid:
$this->{"to{$this->format}"}();
call_user_func_array()
is probably considered more readable than using variable functions, but from what I've read (like here), variable functions tend to out perform call_user_func_array()
.
What did you mean by the variable function being too long?