Is it possible to “escape” a method name in PHP, to be able to have a method name that clashes with a reserved keyword?

后端 未结 2 1983
难免孤独
难免孤独 2021-01-19 21:48

I\'m doing MVC in PHP, and i\'d like to have a list() method inside my Controller, to have the URL /entity/list/parent_id, to show all the \"x\" that belong to that parent.<

2条回答
  •  佛祖请我去吃肉
    2021-01-19 22:17

    You can use __call() method to invoke private or public _list() method which implements your functionality.

    /**
     * This line for code assistance
     * @method  array list() list($par1, $par2) Returns list of something. 
     */
    class Foo 
    {
        public function __call($name, $args) 
        {
            if ($name == 'list') {
                return call_user_func_array(array($this, '_list'), $args);
            }
            throw new Exception('Unknown method ' . $name . ' in class ' . get_class($this));
        }
    
        private function _list($par1, $par2, ...)
        {
            //your implementation here
            return array();
        }
    }
    

提交回复
热议问题