views:

175

answers:

4

I am writing a class that has lots of getters and setters and was wondering on what people thought about the following:

The normal way is to code like the following:

public function setChangeSort($changeSort)
{
    self::$changeSort = $changeSort;
}

public function getChangeSort()
{
    return self::$changeSort;
}

What are your opinions on doing the following:

public function setChangeSort($changeSort) { self::$changeSort = $changeSort; }
public function getChangeSort() { return self::$changeSort; }

I have no problem doing it the orginal way, as that is how it shoul be done. It just seems to take up alot of space in my classon functions that are really obvious in what they do.

Thanks in advance.

+5  A: 

One-line conditional blocks are fine if they're consistent, obvious and intuitive, although I'd encourage spacing them carefully so it's immediately obvious that they are functions rather than some single line of code!

Jeremy Smyth
I would also add consistent as a third qualifier
Steve Weet
Well said! Edited accordingly.
Jeremy Smyth
Thanks, Whether it is the correct way or not I have decided that I will use 1 liners as they are only the very simple getters and setters and i do have 18 of them!
Lizard
A: 

I think magic getters/setters of PHP5 are really helpful when dealing with numerous getters/setters.

http://fr.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

And an example.

<?php
class A {
  private $properties = array();

  public function __set($key, $value) {
    if (is_array($value)) {
      $this->$key = $value;
    } else {
      $this->properties[$key] = $value;
    }
  }

  public function __get($key) {
    if (array_key_exists($key, $this->properties)) {
      return $this->properties[$key];
    }

    return null;
  }
}
?>
SleepyCod
I'm wary of these magic functions.I like a big fuss to be made when something goes wrong. This approach would silently fail if a property didn't exist. Additional error handling code needs to be added to support this.Also this approach gets messy if you have to perform validation on any of the properties.Because of these two conditions I find that the benefit of this approach is quickly lost.
Benedict Cohen
This would be appreciated in a matter of genericity and reusability.This is nice when you want to have a single and common place to hook your validation code, or to hook a plugin system.
SleepyCod
+1  A: 

I would go with the "one-line" variation. But only if all it does is setting and getting a value. if you add a check for null or any other validation, or doing anything other than getting or setting I would go with the first and longer variation to keep the code more readable.

Sorskoot
A: 

I usually threat one-line methods in the same way as the other methods. I don't mind my source code to be N-lines longer if it means it is more readable and maintainable.

I would also suggesto to checkout the following document http://framework.zend.com/manual/en/coding-standard.html IMHO it's the best PHP coding standard reference available so far.

Simone Carletti