Answer the question
In order to leave comments, you need to log in
Is it possible to optimize the function call by name?
I have a piece from a class.
I want that with a different value of $type, the corresponding function would be called for me.
I replaced the original CASE with such a construction, but now the question is, can the call be made even simpler? (in one line)
public function getData($id, $type){
$functionName = 'get' . $type . 'Data';
return $this->$functionName($id);
}
Answer the question
In order to leave comments, you need to log in
public function getData($id, $type){
return $this->{'get' . $type . 'Data'}($id);
}
// или, понятно дело
// ...
$this->{'get' . $type . 'Data'}($id)
// ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question