Reply to Re: Getting and Setting and best practise

Your name:

Reply:


Posted by Moot on 04/20/07 16:13

On Apr 20, 8:39 am, Jim <j...@yahoo.com> wrote:
> 1. Use getters and setters (hate the idea of them though)
> 2. Use __get and __set which I prefer the idea of from a user
> interface point of view though I understand they take no notice of
> member visibility.

Really? Because I've always found __get and __set to be completely
*UN*intuitive from a user interface point of view.

To me, the whole concept and reason for making a class is so that you
can encapsulate logic and present the user (remember, the user of your
class isn't the end user, it's you or another programmer) with a clear
set of "here's what you can do with this object type". Using the
magic get/set functions means that unless you have intimate knowledge
of the internal working of the class, you would have *no clue* as to
what is capable with an object of that class.

Here's an example:
Say you and I are working on a web app. You've created a class to
wrap around a contact in a user's addressbook. I need to get a list
of phone numbers to call for some salespeople, so I say, "hey, I'll
use this handy Contact class my buddy made". I create an object, type
$contact-> and...??? My IDE's autocomplete pops up with a few
functions (save, update, etc...), but how do I get the phone number?
Is it:
$contact->phone;
$contact->phonenumber;
$contact->phone_num;
$contact->...
You get the point.
Whereas with actual defined get/set functions, it would be very
intuitive. I'd see getPhoneNum() in the autocomplete and instantly
know that's what I need to call.

Yes, it is a lot of extra work to make individual get/set functions,
and most of them are going to be near identical copy/paste jobs, but
12 months down the road when you have long since forgotten how
*exactly* your class works, which will be easier? Digging into the
class code to figure out which variable names you're supposed to use,
or letting your IDE's autocomplete pop up and immediately knowing what
get function to call. Take the extra time up front and you'll save
headaches down the line.

--
Moot

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация