drwg89980 2015-08-09 03:22
浏览 24

作为PHP方法的“列表”的一个很好的同义词?

I am building an application where the client provides $_GET['task'] which is limited to list, detail, or edit, and in response the server will provide a list of available records for all IDs, the record for $_GET['id'], or a form to edit record $_GET['id'], respectively.

I would like the string provided by the client to directly match a method in a certain object and not have to map it as a one-off so that I and others will not get confused.

All works well except for list as it is a reserved word.

What is a good synonymy for "list" from both a general user's (since they will see it in the URL) and developers prospective (since they have to make it work)?

  • 写回答

1条回答 默认 最新

  • drb88830 2015-08-09 03:32
    关注

    Well first of all directly exposing your class methods is probably not the best way to set things up. As for list being a reserved word, why limit yourself by not using an api.

    At some point in your application you will need some logic, right. So why not just

      switch( $_GET['mode'] ){
            case 'list':
                do something
            break;
      }
    

    In fact its better to use a class constant, for this like

      Class::MODE_LIST = 'list';
    

    Besides if you really want to name it list you can, use __call like so.

    class API {
    
        const MODE_LIST = 'list';
        const MODE_TASK = 'task';           
        const MODE_EDIT = 'edit';   
    
    
        public function __call($method, $args){
            switch($method){
                case self::MODE_LIST:
                    return $this->_list( $args );
                break;
                case self::MODE_TASK:
    
                break;
                case self::MODE_EDIT:
    
                break;
                default:
                    ///error  -- do some error reporting.
            }   
        }
    
    
        protected function _list( $args ){
    
        }
    
    
    }
    
    $API = new API();
    
    $API->{$_GET['mode']}();
    
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类