|
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
==================
[Back to original message]
|