News Feed
Sections




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

Russell Walker:
Public properties, getters and setters, or magic?
September 26, 2013 @ 09:58:36

Russell Walker has a recent post to his site looking at different ways to work with class properties including using them as public properties or using getters and setters.

Opinion seems to be divided on whether it is good practice to use public properties in PHP classes, or whether to use getter and setter methods instead (and keep the properties private or protected). A sort of hybrid third option is to use the magic methods __get() and __set(). As always, there are advantages and disadvantages to each approach, so let's take a look at them.

He breaks the rest of the post up into three sections, each with a bit of a code example and the common advantages/disadvantages. It's a good overview of the three types and, in the end, it's mostly about what works for your applications. What's his favorite?

My choice then is to use public properties most of the time, but getters and setters for critical settings that I feel need stricter control, would benefit from lazy loading, or that I want to expose in an interface.
0 comments voice your opinion now!
class property getter setter magic public opinion

Link: http://russellscottwalker.blogspot.co.uk/2013/09/public-properties-getters-and-setters.html

blog comments powered by Disqus

Similar Posts

Pádraic Brady's Blog: PHP: Innocent Villagefolk or a Pillagin' Pirate?

DevShed: Building an Image Generator Class with PHP 5

Nessa's Blog: PHP 5.3: Why We’re All Late to the Party

Stefan Koopmanschap's Blog: Your error page can be better

The Coders Lexicon: My Love / Hate Relationship With PHP Traits


Community Events





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


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

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