

Ask HN: Do you use Setters and Getters in PHP?  - JeremyMorgan

I'm ashamed to admit that I have known about setters and getters for a very long time, and used them sparsely, mostly due to what I thought were time constraints.<p>What I learned a couple years ago is, NOT using them can be a bigger loss of time when you're updating something a lot. In an effort to write better stuff I start doing tons more data hiding, and using setters and getters (like most people do in other languages anyway) and found the following things:<p>I can sort of enforce type safety in PHP.
I can create better interfaces and hide implementation better.
Sanitizing the data is far easier. Usually just have to change it one place.
Code is much cleaner in many places.<p>So my question is, are you using them? Is there any good reason not to?
======
mratzloff
In a language like PHP where you can't easily always easily refactor direct
setting of properties (as opposed to Ruby), it makes sense to make anything
where the logic might change in the future into a setter. For simple struct-
like objects with no calculation, direct setting is fine.

(I know this isn't strictly true about PHP with `__set()` and `__get()`, but
that is hardly ideal.)

