tags:

views:

80

answers:

4

I want to set a property value within my class. I can do it without a setter and a getter but I want to know the "correct" way to approach this. This is for a login script so the property and the setter are both private.

Should I just set the property directly or use a setter and a getter?

A: 

This is a matter of opinion. Traditional object oriented programming principles say Yes, but it's really up to you.

Note that you will have to use a setter method if you set your property to private. Setters should always be public, so that other classes and methods can operate on the property. If you don't have any reason to access the property from outside your class though, then don't create a setter, because it really won't matter; you'll always have access to private member variables / properties from within the class.

Brian Lacy
Yes, this is true, thanks for pointing that out. OK, I guess I will probably stay with the setter and getter. I need to echo the value from outside the class so the getter is required.
jim
+1 for using setter since the setter's there for a reason. -1 for setter's need to be public (imo they're just filters that manipulates/validates the data before assigning the property.)
chelmertz
chelmertz, I'd have to agree with you. Currently, I have the setter set as private. I need it that way and it souldn't be able to be altered from outside.
jim
chelmertz, you are of course correct that a setter needn't be public if it performs some special operation on the property. But in that case there would be an obvious need for a setter, and the question should have answered itself. As far as I can tell, such a scenario was never indicated in the original question.
Brian Lacy
+1  A: 

This is probably mainly a matter of personnal preference...

If you have to set the private property from a method of the class, I would say that you can access that property directly.

A reason to use an setter/getter, in that kind of situation, would be if you want to ensure that some condition or calculation is made on the data each time the property is set.


As a sidenote : of course, if your want to access your private property from outside the class, you'll have to use to public setter/getter -- but it doesn't seem to be what you are trying to do here.

Pascal MARTIN
No Pascal, actually it is what I'm trying to do. I need to set a property and read it from outside the class. I currently have the setter private and the getter public.
jim
I'm not providing any type of calculations on any of the data however, just need the out value of the property.
jim
Oh, I guess I didn't understood the question right, then ;; still, what I said remains true : from inside the class, I would access the property directly *(and, of course, use a public getter to access it from outside the class)*.
Pascal MARTIN
Thanks again Pascal. Just to be absolutely clear. You are saying that you would "access" the property, which I take to mean, setting the property inside the class with a setter. Correct?
jim
No, from inside the class, I would use the property directly -- without a setter ; I would use something like this : `$this->_myProperty = 'hello';` *(But, once again : personnal taste !)*
Pascal MARTIN
Ok, I can certainly do that. Thanks for the clarification, Pascal.
jim
You're welcome :-) Have fun !
Pascal MARTIN
A: 

Don't forget that you can use the "magic" __get / __set methods without having to create getters and setters for everything.

magic functions

edit: If the member variables are private or protected you will have to use a getter and setter. If they are public, it's a matter of style - though you should be consistent with the rest of your class.

jasonbar
But bear in mind that they're deadly slow :)
Matchu
Thanks Jason. I think that the magic methods will not work in my case. I need a little more control over what is being set so just decided to use the traditional method.
jim
lol.. I know.. I've experienced them already. PHP needs to do a little work yet to improve performance.
jim
A: 

Setter/Getters allow derived classes to modify/limit/enhance state and behavior of an object more easily. But you can over-do setters/getters and then they become a bit annoying. (Especially in languages that do not hide them).

class Foo {
  protected $x;
  public function __construct($x) {
    $this->setX($x);
  }

  public function setX($x) {
    $this->x = $x;
  }

  public function something($y) {
    // not using a getter here, it's only an example
    echo $this->x * $y;
  }
}

class DoubleFoo extends Foo {
  public function setX($x) {
    parent::setX($x*2);
  }
}

$df = new DoubleFoo(1);
$df->something(5);
VolkerK