News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Berry Langerak's Blog:
Getters and setters evil or necessary evil?
February 14, 2011 @ 09:57:30

In a new post Berry Langerak wonders if the getters and setters commonly used in PHP applications are a necessary evil and if they should be used at all.

Although I do still feel like getters and setters are to be avoided most of the time, it's hard to tell where to use them and where you shouldn't. [...] While writing the code for [PFZ.nl], I started to notice that each and every class in the system had accessors for most, if not all, protected fields. I've started a discussion with my fellow developers, and this is what I've argued.

He mentions a few things including an article he'd written previously on the topic, some of the points behind OOP programming, encapsulation and accessors and collaborators. He also includes several code examples showing what he considers right and wrong ways to do thing (warning, opinions ahead).

All in all, I don't think accessors are evil per se, but you should only ever use the accessors in cross-layer situations. Don't use accessors to build functionality, but only because you need the value to display and/or save.
0 comments voice your opinion now!
getter setter opinion evil necessary oop class


blog comments powered by Disqus

Similar Posts

RawSEO Blog: 5 reasons why oscommerce is a nightmare

David Goodwin's Blog: Storing PHP objects in a database (please no!)

Marco Tabini's Blog: Thoughts for a new year: PHP as the new Java

Reddit.com: Any ideas on what "MVC agnostic" PHP code is?

AppFog: PHP's brighter future? On the "PHP the Right Way" project


Community Events





Don't see your event here?
Let us know!


series code library podcast interview list framework introduction developer configure language threedevsandamaybe bugfix project wordpress api release laravel application community

All content copyright, 2014 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework