In my Class I have methods with the same prefix at the beginning.
sendMessage()
sendPhoto()
sendDocument()
So what I need, is just somehow initialize another method in the class every time when these methods (with prefix) are initialized without putting anything in methods' body. Is there any way to do this out of the box? Some magic php function which triggers every time when method with prefix is invoked...
Use __call()
magic method:
class MagickClass {
public function __call($name, params = []) {
if (strpos($name, 'send') === 0) {
$this->process(strtolower(str_replace('send', '', $name)));
} else {
throw new Exception('Not found', 404);
}
}
private function process($action) {
}
}