News Feed
Jobs Feed
Sections




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

Josh Adell's Blog:
GetSet Methods vs. Public Properties
March 05, 2012 @ 09:50:21

Josh Adell has a new post to his blog talking about a debate between developers over which is the better method - using public properties or getters and setters to work with values on your objects.

I was recently having a debate with a coworker over the utility of writing getter and setter methods for protected properties of classes. On the one hand, having getters and setters seems like additional boilerplate and programming overhead for very little gain. On the other hand, exposing the value properties of a class seems like bad encapsulation and will overall lead to code that is more difficult to maintain. I come down firmly on the get/set method side of the fence.

In his opinion, the getter/setter method provides an explicit interface to the class that describes what it can do and how you can work with it. He gives code examples, comparing the two methods - simple setting of properties on one object and using get*/set* methods on the other. He brings up the point that, if ever in the future you wanted to handle the data for a property differently, say always make it an array or object. He also points out that this still doesn't prevent the setting of new properties directly, so he uses the magic __get and __set to deal with that.

0 comments voice your opinion now!
getter setter public property debate example


blog comments powered by Disqus

Similar Posts

Rob Allen's Blog: Modules (Zend Framework)

Web Development Blog: Create PDF documents Online with TCPDF

Sebastian Bergmann's Blog: Data Providers in PHPUnit 3.2

Padraic Brady's Blog: Unit Testing: Multiple Assertions And Lazy/Shallow Testing Are Evil

David Otton's Blog: PHP Tip: Classes Arenít Derived From stdClass


Community Events











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


application facebook podcast framework introduction security install hhvm symfony2 opinion overview example composer language hack code unittest release component package

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