Getters and setters are a style of programming where object properties are encapsulated as method calls, and the properties are made private or protected. Recently, PJE posted a comparison of Python and PJE where he said that "Getters and setters are evil". As a KISS sort of guy, I concurred. Jonathan then asked for clarification, asking:
Surely its a larger waste of time having to change $blah->field = 10; to $blah->setField(10); because 3 months down the line further functionality is required?
Here's my response:
Of course get/set are not evil. You have a choice, but my experience has not been positive. Maybe because my designs are lucky enough not to require this sort of refactoring (eg. ADOdb, in use by thousands of developers for 4+ years). Also as a general rule, I believe that properties should avoid side-effects.
So I'm not impressed with the following example:
$blah->field = 10; to $blah->setField(10);
In over 20 years of programming, i have rarely found the above to be the case. 99% more likely is the realisation that you need to change and cope with more special cases that did not occur to the designer. E.g.
$this->field = 10 to
$this->ChangeFieldMode(10, $newCondition1, $newCondition2);
Setters/Getters cannot fix this type of refactoring which is the most common one i face; so to me set/get are a waste of time. For the last 1%, i have found multi-file search/replace in a good text editor sufficient, or PHP 5's new __get() and __set().
What do you think?
![]()

