News Feed
Sections




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

DZone.com:
How to remove getters and setters
February 23, 2011 @ 12:02:17

On DZone.com's Web Builder Zone Giorgio Sironi has posted a few methods you can use to help get rid of getters and setters in your OOP PHP applications.

Encapsulation is (not only, but also) about being capable of changing private fields. If you write getters and setters, you introduce a leaky abstraction over private fields, since the names and number of your public methods are influenced coupled to them. They aren't really private anymore:

To show his alternatives, he uses a sample "User" class with a whole list of private properties. There's initially a get/set for the nickname and password values, but he suggests a few replacements:

  • passing values in through the constructor
  • using the "Information Architect" pattern to have the most responsible method handle the value setting
  • the "Double Dispatch" method that uses dependency injection
  • using the Command pattern and changesets of data

He also briefly mentions the Command-Query Responsibility Segregation (CQRS) method, but doesn't have any code example to go with it.

0 comments voice your opinion now!
getter setter opinion remove replacement


blog comments powered by Disqus

Similar Posts

Davey Shafik's Blog: Making the case for PHP

Reddit.com: What non-PHP stuff should a PHP developer know?

Richard Thomas' Blog: Frameworks are like Hammers

Cal Evans' Blog: Six ways to be a better client for your developer - Point 2

Jani Hartikainen's Blog: Creating a simple abstract model to reduce boilerplate code


Community Events





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


list code conference tips introduction framework series laravel zendserver api community bugfix deployment threedevsandamaybe podcast interview symfony voicesoftheelephpant release language

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