-5

Possible Duplicate:
Is it really that wrong not using setters and getters?
Why use getters and setters?

I have been always wondering why are people using getters/setters in PHP instead of using public properties?

From another question, I've copied this code:

<?php
class MyClass {
  private $firstField;
  private $secondField;

  public function __get($property) {
    if (property_exists($this, $property)) {
      return $this->$property;
    }
  }

  public function __set($property, $value) {
    if (property_exists($this, $property)) {
      $this->$property = $value;
    }

    return $this;
  }
}
?>

I see no difference between this and using public fields.

Well, I know it may help us to validate data in both getter and setter, but the example above just doesn't fit it

Community
  • 1
  • 1
genesis
  • 50,477
  • 20
  • 96
  • 125

2 Answers2

7

Getters and setters are used in order to prevent code outwith the class from accessing implementation details. Maybe today some piece of data is just a string, but tomorrow it has be created by joining two other strings together and also keeping a count of the number of times the string is retrieved (OK, contrived example).

The point is that by forcing access to your class to go through methods, you're free to change how your class does things without impacting other code. Public properties don't give you that guarantee.

On the flip side, if all you want to do is hold data, then public properties are fine, but I think that's a special case.

Rory Hunter
  • 3,425
  • 1
  • 14
  • 16
  • Oh, so the main reason is that you can start validating the value before setting without changing it on all occurences of setting the property? – genesis Apr 09 '12 at 16:00
  • Yeah, that's another good reason. Validation code in one place is a Very Good Thing. – Rory Hunter Apr 09 '12 at 17:10
1

With getters and setters you get the control over the properties of a class. Look like this example:

<?php
class User
{
  public function $name;

  public function __construct($name)
  {
    $this->setName($name);
  }

  public function setName($name )
  {
    if (!preg_match('/^[A-Za-z0-9_\s]+$/')) {
      throw new UnexpectedValueException(sprintf('The name %s is not valid, a name should only contain letters, numbers, spaces and _', $name);
    }
    $this->name = $name;
  }
}
$foo = new User('Foo'); // valid
$foo->name = 'Foo$%^&$#'; // ahh, not valid, but because of the public property why can do this

If you make the property protected, or private, this can't be done and you can have control over what is in the property.

Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • Hmm, this doesn't fit my example code - you actually have new setter for each property. I don't. But I get what you mean – genesis Apr 09 '12 at 16:01
  • @genesis Yes. I never use the magic setters/getters. A reason why the magic methods is useful is if you want to store the items in one property in the class, for instance in a Registery or Service Container. – Wouter J Apr 09 '12 at 16:56
  • Example of the above comment: http://snipplr.com/view/64458/a-simple-registery/ – Wouter J Apr 09 '12 at 17:05