accessor pros/cons (was [nycphp-talk] object getter/setter methods)
Allen Shaw
ashaw at iifwp.org
Fri Mar 11 12:56:38 EST 2005
Derek's question (balancing the value and trouble of creating accessor
functions for each variable) did not *exactly* match the topic of the
article he mentioned: deciding on the design-wisdom of accessor
functions in general, assuming that they often have the effect of
allowing other code to meddle in what is "really" the business of the
class.
The article says:
> 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.
>
> By designing carefully and focusing on what you must do rather than
> how you'll do it, you eliminate the vast majority of getter/setter
> methods in your program. /Don't ask for the information you need to do
> the work; ask the object that has the information to do the work for you./
>
Seems to me he has a point, and as a novice programmer I'm persuaded to
try and avoid reading/manipulating class properties as much as possible
and write the class do whatever the calling code might be doing with
that property. Did anyone read the article and want to comment on it?
- Allen
Derek DeVries wrote:
>For every object that I create I end up making countless getter/setter
>methods. My User class is littered with methods like getUsername(),
>getEmail(), etc. This can add up to thousands of lines of code for my
>objects. Is there a better way around this? A method like this would
>cut down on code:
>
>/**
> * Get the value of the given property.
> * eg. $username = $user->get('username');
> * @param string $var Name of the property
> * @return mixed
> */
>function get($var)
>{
> return isset($this->{$var}) ? $this->{$var} : null;
>}
>
>However this would allow for access to any of the properties, which I
>don't necessarily want. Has anyone tried using the above approach to
>success/failure? I suppose another approach would be to make all of
>the accessable properties in an associative array. I am aware of
>php5's __get/__set magic methods but am currently still using v4. I
>like the idea of having the specific getX()/setX() methods in my API
>because it seems clearer for others to read. However I feel that I'm
>writing a lot of unnecessary code.
>
>I've run into this article titled: Why getter and setter methods are
>evil. http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html
>
>This is a java article that advocates eliminating accessors. I'm not
>sure if I want to go to that extreme, but I guess there are a few good
>points.
>_______________________________________________
>New York PHP Talk Mailing List
>AMP Technology
>Supporting Apache, MySQL and PHP
>http://lists.nyphp.org/mailman/listinfo/talk
>http://www.nyphp.org
>
>
>
--
===========================================================
Allen Shaw ashaw at iifwp.org
IIFWP Data and 914.631.1331 x.106
IT Services http://www.iifwp.org
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20050311/07a8c10e/attachment.html>
More information about the talk
mailing list