On Friday, June 7, 2002, at 12:50 AM, Analysis & Solutions wrote:
> On Thu, Jun 06, 2002 at 04:57:42PM -0400, Erik Price wrote:
>> I have a method in my class that essentially unsets an array element.
> Uh, I know you're a sharp guy, but I've got to ask anyway. If it's
> doing such a simple thing, why do you need a method to do it? Why not
> go ahead and unset the array directly?
Well, for two reasons -- one, it does a few more things than just unsets
the element (such as checks for certain condition). The main reason,
however, is that I'm trying to get into the habit of using
accessor/mutator methods in my classes. PHP lets me directly manipulate
attributes from the class, but Java doesn't, and I'm learning Java in my
spare time, so... I'm basically foregoing some of the luxuries of PHP in
order to be more strict in my coding.
>> Now, originally I was doing all of this method calling from the script
>> itself. For each variable POSTed by the user, I call the first method.
> Why not have the function automatically check each variable in POST (you
> know, do a while loop for each $_POST) rather than have to call the
> function over and over? That way you can call the thing once and you're
> done. Plus, you can then either put that second, clean up, step in here
> or have it a separate function called at the end of this POST check
Yes, that's an excellent idea. It's pretty much what I ended up doing
in my class, though I didn't loop through the POST data (rather, I wrote
my code to anticipate certain POST data and it acts upon that). IOW I
went with the "encapsulation" over the "modularity" for my class. I can
always add another method for another situation where the conditions are
different if need be.
>> But now that I have a second "clean-up" method that needs to be called,
>> how should I go about it? Should I have one "master" method in the
>> class that is called from the script, and itself does all the work?
>> This keeps all of the work in the class, and out of the calling script.
>> Should I keep the class free of code that only executes according to
>> POST variables from the user, and keep this kind of thing in the
>> script. That way, the object's class is more flexible, and can be used
>> in more contexts.
> I tend to use classes for reusable purposes throughout my projects.
> So, I wouldn't bog it down with things you're only going to use in one
> place. Perhaps put this second, clean-up, process in an extension of
> the primary class?
Well, the way I end up doing it, there are a few methods that do actual
work and then there are... how should I put it... "master methods" that
are invoked from the script and call these "worker methods" depending on
the situation. True, I will have to create a new "master method" later
if I need to use this class again in a different context with different
POST data, but all the work is really done, the "master method" simply
calls the appropriate "worker methods" and can be whipped up in no time.
Web Developer Temp
Media Lab, H.H. Brown
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php