SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question When to use setters and getters

    Should I use a setter and getter function for every variable that is provided by the user?

    i.e. I am writing a database class that has a:

    public function select($fields, $table, $where, $orderby, $limit)

    should I process each one of those function parameters i.e:

    public function select($fields, $table, $where, $orderby, $limit){

    $this->setFields($fields);
    $this->setTable($table);
    $this->setWhere($where);
    $this->setOrderby($orderby);
    $this->setLimit($limit);

    //Rest of code here

    Thanks in advance!!!
    Global

    P.S. I am aware that PHP frameworks offer these classes but I would like to know the best practices for myself...

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK I was up late last night and my brain was little fried I do realise now that the variables should already be processed by the time they are included in a select mysql query.

  3. #3
    SitePoint Wizard Wolf_22's Avatar
    Join Date
    Jul 2005
    Posts
    1,714
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Been there a few times...

  4. #4
    SitePoint Wizard PHPycho's Avatar
    Join Date
    Dec 2005
    Posts
    1,201
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Global Cooling View Post
    Should I use a setter and getter function for every variable that is provided by the user?

    i.e. I am writing a database class that has a:

    public function select($fields, $table, $where, $orderby, $limit)

    should I process each one of those function parameters i.e:

    public function select($fields, $table, $where, $orderby, $limit){

    $this->setFields($fields);
    $this->setTable($table);
    $this->setWhere($where);
    $this->setOrderby($orderby);
    $this->setLimit($limit);

    //Rest of code here

    Thanks in advance!!!
    Global

    P.S. I am aware that PHP frameworks offer these classes but I would like to know the best practices for myself...
    AFAIK:
    Setters are used when
    - you don't have publicly accessible properties
    - you want to validate or do something extra with the properties

    Getters are used when
    - you want to validate or do something extra with the properties while accessing

    If you have Zend Studio 7.0, it can generate getters and setters based on the properties.


Tags for this Thread

Bookmarks

Posting Permissions

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