1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Generic or individual functions

Discussion in 'PHP' started by screenmates1, Apr 25, 2006.

  1. #1
    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; }

    OR:
    ===

    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.

    Thanks,
     
    screenmates1, Apr 25, 2006 IP
  2. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I guess what I'm going to ask is something a little different... is there a reason you're using an array of values as opposed to a class? I mean, they look like they're all related, so why not use a class?

    Either way, why not just access them directly? There's no real reason to use get and set functions, I don't think... in theory, functions like these make sense, but only if you're going to be doing 'extra' stuff with the data.

    It looks like you just reading / writing the info, so why not access it directly?
     
    TwistMyArm, Apr 25, 2006 IP
  3. screenmates1

    screenmates1 Member

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Yes, it is a class. And it has descendant classes derived from this ancestor that has more elements in the $var array which is an array of arrays (grouped). A single array is used to hold all vars to reduce clutter and for the ability to pass certain keys/values as 'groups' to other classes (sub-arrays). Most classes out there such as Smarty, PEAR... all use arrays to hold class vars as a single point of access. In case of my class, each value is handled differently and I'm thinking generic functions are not the right way to go. Any thoughts?
     
    screenmates1, Apr 25, 2006 IP
  4. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If each value is going to be handled differently then for sure, use separate functions.

    It's either that or lots of switch cases, so you may as well use proper functions.
     
    TwistMyArm, Apr 25, 2006 IP
  5. screenmates1

    screenmates1 Member

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Thanks, much.
     
    screenmates1, Apr 25, 2006 IP