2009年5月29日星期五

Re: [fw-mvc] get and set accessors

Ha, I was in the middle of writing some getter/setter methods when I saw your message :)

I'm reading the article now, but from what I am gathering it seems that the author is afraid that programmers use the getter/setter methods to override or hijack the implementation details of a class.

Like take Zend_Db_Table, for instance. I could write something like this:

$table->getAdapter()->query($myCustomSql);

Generally, I think using getters in this fashion can lead to problems down the road. But there are also valid uses for getter methods, like if you need to quote a string:

$table->getAdapter()->quote('foo');

Also, getters/setters are great for unit testing, as you can inject mock objects into your classes.

-Hector


On Fri, May 29, 2009 at 1:12 PM, Ed Lazor <edlazor@internetarchitects.biz> wrote:
Probably more of a general coding debate, but...

What's your opinion on the pros and cons of using get and set
accessors?  What's the Zend Framework strategy on this?  Do you have a
preference one way or the other?

Any examples you can give that support your opinion would be great.

Here's one article suggesting that "getter and setter methods are
evil".  It's written toward Java, but it seems like the debate easily
applies to PHP / ZF.
http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html.
Do you agree with what the author says?

-Ed


没有评论: