user1032531
user1032531

Reputation: 26281

A good synonymy for "list" as a PHP method?

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)?

Upvotes: 0

Views: 60

Answers (1)

ArtisticPhoenix
ArtisticPhoenix

Reputation: 21671

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']}();

Upvotes: 1

Related Questions