PHP - Getters and Setters
April 27th, 2006
There is alot of talk about Getters and Setters being evil. The major argument is encapsulation, basically if you need that information outside the class, maybe that class should be doing the work on the information, not you. This sounds reasonably valid to me in a pure OO sense but in reality, sometimes we want to be able to just set a variable’s value.
In PHP, it seems alot easier to leave the variable public and set it directly rather than using a setVar() method. I’m of the belief that using the setVar() method will save you time later on when you want to start creating fluid interfaces. If you set a value directly, you can’t choose what you want to return. Consequently, hiding setters and getters with the use of the __get and __set magic functions can also make fluid interfaces hard to implement.