Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005

    Unanswered: Generic or individual functions

    I have an array of vars in a class and am wondering how to provide an API for them - whether to use two generic functions (set/get) or one individual function for each array element. Eg:

    $vars = array( 'userid' => $userid,
    'password' => $password,
    'nickname' => $nickname,
    'address' => array( 'street' => $street,
    'city' => $city,
    'state' => $state,
    'zip' => $zip ),
    'gender' => $gender,
    'age' => $age,
    ... );

    function getVar( $key )
    { return $this->vars[ $key ); }

    function setVar( $key, $value )
    { $this->vars[ $key ] = $value; }


    function getUserId()
    { return $this->vars[ 'userid' ]; }

    function setUserId( $userid )
    { $this->vars[ 'userid' ] = $userid; }

    function getPassword()
    function setPassword()
    function getAddress()
    function setAddress()

    The $vars array has over 20 elements. Which approach would be neater and recommended from the point of view of long-term maintainability.

    Last edited by screenmates; 04-25-06 at 20:47.

  2. #2
    Join Date
    Jan 2005
    i am by no means an expert, but i prefer to build functions from the bottom up. it may make the code a bit slower, but it makes reading and writing your scripts much easier. i would write the low level functions like your first ones. then even if you want to write a higher level function like get password the function can call the other, like:

    function get_password(){
    return getVar( 'password');

    the advantage of this approach is if you wanted to change your code, so your data was in a database or xml file you would only have to change the 2 functions.

  3. #3
    Join Date
    Nov 2004
    out on a limb
    Provided Answers: 59
    if the class is being by you, or you are setting the standard then do what ever you feel comfortable with.

    Probably OOP purists would insist on separate functions to se set each property, and they'd probably be right.

    Personally I'd compromise, some information that is stored in multiple items could be done as a single call .... details of an address come to mind. Some things probably should naturally be single set/get's. eg date of birth.

    If you wanted to, you could offer both styles, and call the indivudal set/get functions from the "bulk" function.

    there is rarely a single "correct" answer to any problem in systems development.
    I'd rather be riding on the Tiger 800 or the Norton

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts