 | News Feed |
 | Jobs Feed |
Sections
|
| feed this: |  |
Josh Adell's Blog: GetSet Methods vs. Public Properties
by Chris Cornutt 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.
voice your opinion now!
getter setter public property debate example
Rob Allen's Blog: Sublime Text 2 Snippet for PHP getter and setter generation
by Chris Cornutt January 03, 2012 @ 09:54:23
In a quick new post to his blog, Rob Allen has shared a snippet for the Sublime Text 2 editor to make creating getters and setters for your class simpler (dynamically too).
As with a lot of editors, Sublime Text supports snippets which are essentially text expansions of a short phrase into more text. I needed to create a few getXxx() and setXxx() methods for some properties of a class and decided that the easiest way to do this would be with a snippet.
Included in the post is the code you'll need to put into the snippet - a simple find (regular expression based) looks at the currently selected variable and expands out the getter and setter for it. For more information on the Sublime Text 2 editor, see the product's website.
voice your opinion now!
sublimetext2 editor snippet getter setter code
Bence Eros' Blog: Getters, setters, performance
by Chris Cornutt July 12, 2011 @ 11:39:18
Bence Eros has put together a new post to his blog looking at some of the results he's found from performance testing the use of getters and setters in PHP.
The usage of getter and setter methods instead of public attributes became very popular in the PHP community, and it's going to become the standard coding convention of so many PHP libraries and frameworks. On the other hand many developers - including me too - strongly unrecommend such convention, because of its performance overhead. I wanted to make some performance comparison for years, and today I had time to do that. In this post I would like to show what I found.
He starts with a question every developer asks as their working in their application - why and when should they use getters and setters for their classes. He talks about using them as primary functionality or as fallbacks only when needed. He includes the simple benchmarking script he used to compare accessing/setting public attributes directly and using a getter/setter to do the same. The results aren't very surprising if you think about the "magic" that has to happen for getters and setters to work. See the rest of the post for those numbers.
voice your opinion now!
getter setter performance benchmark compare magic
Shawn Stratton's Blog: Accesors and Religion
by Chris Cornutt May 17, 2011 @ 14:46:32
Shawn Stratton has a new post to his blog talking about a topic he calls "flame bait" - the use of accessors in PHP applications (getters and setters) to access class properties.
Objects have properties which sometimes need special logic on how they are retrieved and set. There are several solutions to this and everybody has a different view point about which is correct, none are pretty and all have drawbacks which range from writing extra code, creating something that has poor extensibility, or has issues with consistency. These don't even breach the issues with IDE code completion and analysis. Lets look at some of these solutions.
The solutions are the direct access to the class properties (which can cause some painful inconsistencies when things get complicated) and the alternative of setting up getters/setters for everything. Shawn's alternative makes use of the __get and __set magic methods to catch the property values being set and handle them correctly, developers being none the wiser.
voice your opinion now!
accessors opinion getter setter class property
DZone.com: How to remove getters and setters
by Chris Cornutt 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.
voice your opinion now!
getter setter opinion remove replacement
Berry Langerak's Blog: Getters and setters evil or necessary evil?
by Chris Cornutt 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.
voice your opinion now!
getter setter opinion evil necessary oop class
Michael Kimsal's Blog: magic __get and __set style?
by Chris Cornutt December 21, 2010 @ 09:36:05
In this recent post to his blog Michael Kimsal asked about the usefulness of the __get and __set magic methods and the sort of code he usually sees along with it.
For a long time I've held that __get and __set in PHP were not all that hot - mostly because it's solely error handling. There's no way to deal invoke __get or __set behaviour for properties that are defined on a class. [...] For those who insist on using __get/__set, I *typically* see this [messyy] sort of style code. The effect is to cram a bunch of unrelated code in to the __get/__set overloading methods.
He suggests an alternative to the "cram it all in __get" approach - callbacks to other methods in the class to handle the different variable possibilities. In the comments there's other suggestions on how to get the same job done differently - mapping property values with __call, valid uses for getters/setters and a pointer to an internals discussion RFC about this same thing.
voice your opinion now!
getter setter oop class behavior opinion
PHPImpact Blog: No need for set/get methods in Python
by Chris Cornutt August 18, 2008 @ 12:06:37
Federico compares two languages in this new post to the PHP::Impact blog today - PHP and Python - in their need for "getters" and "setters".
Python code doesn't typically use the get and set methods so common in PHP. Normally, when writing PHP code, you carefully protect your instance variables by making them private, so callers can only interact with them via getter and setter methods. [...] Python's solution to this problem is more readable, it has a construct called a "property".
He compares two blocks of code that do the same thing - set properties on the object with the PHP side doing a bit more error checking (seemingly) than the Python side. They both apply a title property to a book object.
voice your opinion now!
python compare getter setter example
|
Community Events
Don't see your event here? Let us know!
|