SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Mar 29, 2008, 19:32 #1
- Join Date
- Feb 2007
- Location
- UK
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Using set/get methods vs just accessing properties
Hi,
I couldn't find anything on this, probably because I dont have a clue what to search for!
Can someone please explain why, in a class's method, I should use a seperate method like setSomething() to set/edit a class's property rather than accessing the property directly. For example, why use:
PHP Code:$this->addErrorMessage($msg);
PHP Code:$this->errorMessages[] = $msg;
Mike
-
Mar 30, 2008, 01:38 #2
- Join Date
- Aug 2007
- Posts
- 365
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
If the varible doesn't have to be an specific type and no restrictions on getting and setting I use $object->value
Otherwise I use $object->setValue( $value );
-
Mar 30, 2008, 04:52 #3
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It's about being explicit about the interface. With direct property access, it is hard to find out where the change was made. If you need to change your code, it's rather hard to localise all the places, where the property is used from. Another benefit of accessors (As getter/setters are called), is that you can stuff additional code in there, to execute when called. With a getter, you might implement lazy-loading and cache the result, without the caller knowing about it. With setters, you might implement logging, validation an so on. Even if you don't need this immediately, if you use accessors from the beginning, it's easy to add in later, since you don't have to change the interface.
More generally, whenever an object needs access to another object's properties, it is a sign of badly factored code. In object oriented code, you should strive to keep functionality together with the data upon which it acts. If you do so, you'll find that you may not even need the accessor in the first place, and can then make the property entirely private.
-
Mar 30, 2008, 07:37 #4
- Join Date
- Feb 2007
- Location
- UK
- Posts
- 591
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the perfect reply, kyberfabrikken. That explained exactly what I was confused about.
-
Mar 30, 2008, 14:25 #5
- Join Date
- Mar 2005
- Posts
- 423
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You may what to read an interesting article on the subject by java guru Allen Holub, in an article he calls Getters and Setters are evil. Don't necessarily agree, but an interesting read anyway.
getters and setters are evil.
It's also in his book, Holub on Patterns.
-
Mar 31, 2008, 17:00 #6
- Join Date
- Sep 2006
- Posts
- 232
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I think what Allen Holub is trying to say is that getters and setters accessors are not evil by nature, but they can turn evil if you add them without any clear justification.
And he explains why:
It's best to minimize data movement as much as possible. My experience is that maintainability is inversely proportionate to the amount of data that moves between objects. Though you might not see how yet, you can actually eliminate most of this data movement.
-
Apr 1, 2008, 02:47 #7
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
-
Apr 1, 2008, 04:07 #8
- Join Date
- Nov 2005
- Location
- Melbourne, Australia
- Posts
- 713
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Just thought I'd chime in and throw my $0.02 cents into this thread (whaddya mean we don't have 2c coins in Oz?!
).
When I started writing OO code I never used accessors at all. Everything was done by assigning to properties. When I gained a little experience and saw the benefits of using accessors I used them for everything, but I didn't make them all public.
Now (in the past 8 months or so) I only use accessors for providing public or protected access to fields. Anything which I assign to in private I'm more than happy to make that assignment directly. I think this is clean and reduces all the setter clutter you often have in OO code.
When it comes to testing your code having the accessors for the public API makes a massive amount of difference too.
-
Apr 2, 2008, 12:30 #9
- Join Date
- May 2004
- Location
- Central USA
- Posts
- 806
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I pretty much agree with what everyone here has said so far, but I'll offer you a different reason to use accessor methods as well: change.
By using a method, you can easily change the underlying implementation without breaking any code. So for example if you were to one day change the $errorMessages array to be an $error object instead, you would simply change your method to something like:
PHP Code:public function addErrorMessage($msg) {
$this->error->add($msg);
}
Stackbox CMS - Full edit-on-page drag-and-drop CMS
Autoridge - Vehicle information & maintenance part numbers
Twitter | Blog | Online Javascript Compressor
Bookmarks