dongwu5801 2018-05-07 13:31
浏览 28
已采纳

用于在php中自动调用的魔术函数

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...

  • 写回答

1条回答 默认 最新

  • doutuohan6606 2018-05-07 14:05
    关注

    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) {
    
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面