You are here: Re: Getting and Setting and best practise « PHP Programming Language « IT news, forums, messages
Re: Getting and Setting and best practise

Posted by Jerry Stuckle on 04/20/07 17:08

Jim wrote:
> Hi guys,
>
> I have an object which represents an "item" in a CMS "component" where
> an "item" in the most basic form just a field, and a "component" is
> effectively a table.
>
> "item" objects can be created and then added to "component" objects to
> build up the component definition.
>
> My dilemma comes in deciding how to read/write data to the "item"
> object.
>
> I figure I can either:
>
> 1. Use getters and setters (hate the idea of them though)

But it's the right way to go. Among other things, it makes the code
much easier to maintain in the future.

> 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.

You can do it - but now you have to validate the parameter - is it an
actual value? And it will be harder to maintain in the future - more
code to worry about when you add/delete values. Validation also becomes
much more complicated.

> 3. Pass all the required parameters to one function and do all the
> validation there - inflexible, unintuitive and more work in the long
> run in my opinion.
>

About the same as using __set, isn't it?

> I'm leaning towards the __get and __set route so I could do:
>
> $item = new Item('user');
>
> // could throw exceptions in the functions that do validation
> $item->required = true;
> $item->label = 'User';
> $item->description = 'A description of the item';
>
> // all validation on the item is done so just add it
> $component->add_item($item);
>
>
> Does this make sense? I'm interested in hearing what other methods
> people would choose and why.
>
> Thanks for any input.
>
> Jimmy.
>

From the point of clarity, ability to later modify the code, etc., I
much prefer getters and setters. Sure it means you may have a lot of
functions - but there won't be the independence on other code you have
with other ways. It will probably be faster, also.



--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this 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

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