Triggering __call() in PHP even when method exists

后端 未结 2 981
借酒劲吻你
借酒劲吻你 2020-12-30 02:31

The PHP documentation says the following about the __call() magic method:

__call() is triggered when invoking inaccessible methods in an

2条回答
  •  粉色の甜心
    2020-12-30 02:39

    Why not just make all your methods protected and call them using __call():

     class bar{
        public function __call($method, $args){
            echo "calling $method";
            //do other stuff
            //possibly do method_exists check
            return call_user_func_array(array($this, $method), $args);
        }
        protected function foo($arg){
           return $arg;
        }
     }
    
    $bar = new bar;
    $bar->foo("baz"); //echo's 'calling foo' and returns 'baz'
    

提交回复
热议问题