Re: [PHP] OOP style question
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 > stuff. 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. >> >> OR... >> >> 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 >> calling >> 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. Thanks Dan, Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP style question
Hey Erik: It's getting late, so I may not be thinking clearly... 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? > 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 stuff. > 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. > > OR... > > 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 calling > 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? Good night (or good morning by the time y'all read this), --Dan -- PHP classes that make web design easier SQL Solution | Layout Solution | Form Solution sqlsolution.info | layoutsolution.info | formsolution.info T H E A N A L Y S I S A N D S O L U T I O N S C O M P A N Y 4015 7 Av #4AJ, Brooklyn NY v: 718-854-0335 f: 718-854-0409 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] OOP style question
Thanks Scott, That's kind of what I thought -- that there's no cut-and-dried answer to the question I asked. I'm actually amazed that it came out coherently enough for someone to understand it and answer! Your solution is a good one. On the one hand this is less modular, since it tests for certain conditions that have to exist (i.e., the POSTed variables) and thus requires a completely different method for completely different conditions, but on the other hand, it does nicely keep all of the code in the class so that the script doesn't get bogged down with many method calls. I do one call to dothis(), which does all my work, and then I'm done. That's what I'll go with, thank you. Erik On Thursday, June 6, 2002, at 05:09 PM, Scott Hurring wrote: > It depends highly on what you're doing and how you're doing it :) > > If somethign needs to be done *always*, just throw it into the > Object so that the user won't have to call it explicitly, > however if you want to provide fine-grained control over > how/when/why things are cleaned-up, you might want to > keep it "public" and let the user call it explicitly. > > > For example, if clean-up is automatic, and only once-per-session, > this might be somewhat along the lines of what i think you're > asking for::: > > function dothis($form) > { > // . do whatever to POSTED variables > > $this->cleanup($form); > } > > function cleanup($form) > { > // Only run once per instance > if ($this->cleanup_called) > return 1; > $this->cleanup_called = 1; > > // ... do cleanup > } > > --- > Scott Hurring > Systems Programmer > EAC Corporation > [EMAIL PROTECTED] > Voice: 201-462-2149 > Fax: 201-288-1515 > >> -Original Message- >> From: Erik Price [mailto:[EMAIL PROTECTED]] >> Sent: Thursday, June 06, 2002 4:58 PM >> To: [EMAIL PROTECTED] >> Subject: [PHP] OOP style question >> >> >> I'm trying to solve my earlier-posted dilemma of a class attribute >> (array) that is "remembering" elements that should have been >> unset(). >> The good news is that, according to some testing I've been >> doing (I have >> separated the relevant code and am directly testing it), it >> looks like >> PHP is behaving as expected -- I really hope that it's just >> an error on >> my part, so that I can fix it. >> >> But out of this exercise I have begun to wonder something. >> If someone >> who is better-schooled than I in object oriented programming >> style could >> respond I would be very grateful. >> >> I have a method in my class that essentially unsets an array >> element. >> The frequency with which this method is called varies >> depending on the >> circumstances, so i can't "hard-code" the solution to this >> problem. But >> after the method is done being called (however many times it need be >> called), a second method needs to be called. Think of this >> second one >> as a "clean up" method, that needs to be called anytime the >> first method >> is called, but ONLY ONCE per script instance, no matter how >> many times >> the first method was called. This means that I can't just >> call method >> #2 every time I call method #1. >> >> 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. >> 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. >> >> OR... >> >> 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 calling >> script. That way, the object's class is more flexible, and >> can be used >> in more contexts. >> >> Again, this is really a question of style -- I can get it to >> work either >> way. I'm just wondering if a class should be written to >> handle ALL CODE >> related to its class, and keep most of the work in "private" methods >> (not really enforced in PHP but whatever), or whether the >> class should >> be written so that it has a lot of public methods that can be called >> from the script, which means that the class is more flexible >> in the long >> run? >> >> A question of encapsulation vs modularity, it would seem. >> >> Your thoughts are gratefully accepted. >> >> >> >> Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] >> >> >> -- >> PHP General Mailing List (http://www.php.net/) >> To unsubscribe, visit: http://www.php.net/unsub.php >> > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] OOP style question
It depends highly on what you're doing and how you're doing it :) If somethign needs to be done *always*, just throw it into the Object so that the user won't have to call it explicitly, however if you want to provide fine-grained control over how/when/why things are cleaned-up, you might want to keep it "public" and let the user call it explicitly. For example, if clean-up is automatic, and only once-per-session, this might be somewhat along the lines of what i think you're asking for::: function dothis($form) { // . do whatever to POSTED variables $this->cleanup($form); } function cleanup($form) { // Only run once per instance if ($this->cleanup_called) return 1; $this->cleanup_called = 1; // ... do cleanup } --- Scott Hurring Systems Programmer EAC Corporation [EMAIL PROTECTED] Voice: 201-462-2149 Fax: 201-288-1515 > -Original Message- > From: Erik Price [mailto:[EMAIL PROTECTED]] > Sent: Thursday, June 06, 2002 4:58 PM > To: [EMAIL PROTECTED] > Subject: [PHP] OOP style question > > > I'm trying to solve my earlier-posted dilemma of a class attribute > (array) that is "remembering" elements that should have been > unset(). > The good news is that, according to some testing I've been > doing (I have > separated the relevant code and am directly testing it), it > looks like > PHP is behaving as expected -- I really hope that it's just > an error on > my part, so that I can fix it. > > But out of this exercise I have begun to wonder something. > If someone > who is better-schooled than I in object oriented programming > style could > respond I would be very grateful. > > I have a method in my class that essentially unsets an array > element. > The frequency with which this method is called varies > depending on the > circumstances, so i can't "hard-code" the solution to this > problem. But > after the method is done being called (however many times it need be > called), a second method needs to be called. Think of this > second one > as a "clean up" method, that needs to be called anytime the > first method > is called, but ONLY ONCE per script instance, no matter how > many times > the first method was called. This means that I can't just > call method > #2 every time I call method #1. > > 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. > 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. > > OR... > > 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 calling > script. That way, the object's class is more flexible, and > can be used > in more contexts. > > Again, this is really a question of style -- I can get it to > work either > way. I'm just wondering if a class should be written to > handle ALL CODE > related to its class, and keep most of the work in "private" methods > (not really enforced in PHP but whatever), or whether the > class should > be written so that it has a lot of public methods that can be called > from the script, which means that the class is more flexible > in the long > run? > > A question of encapsulation vs modularity, it would seem. > > Your thoughts are gratefully accepted. > > > > Erik > > > > > > > > Erik Price > Web Developer Temp > Media Lab, H.H. Brown > [EMAIL PROTECTED] > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] OOP style question
I'm trying to solve my earlier-posted dilemma of a class attribute (array) that is "remembering" elements that should have been unset(). The good news is that, according to some testing I've been doing (I have separated the relevant code and am directly testing it), it looks like PHP is behaving as expected -- I really hope that it's just an error on my part, so that I can fix it. But out of this exercise I have begun to wonder something. If someone who is better-schooled than I in object oriented programming style could respond I would be very grateful. I have a method in my class that essentially unsets an array element. The frequency with which this method is called varies depending on the circumstances, so i can't "hard-code" the solution to this problem. But after the method is done being called (however many times it need be called), a second method needs to be called. Think of this second one as a "clean up" method, that needs to be called anytime the first method is called, but ONLY ONCE per script instance, no matter how many times the first method was called. This means that I can't just call method #2 every time I call method #1. 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. 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. OR... 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 calling script. That way, the object's class is more flexible, and can be used in more contexts. Again, this is really a question of style -- I can get it to work either way. I'm just wondering if a class should be written to handle ALL CODE related to its class, and keep most of the work in "private" methods (not really enforced in PHP but whatever), or whether the class should be written so that it has a lot of public methods that can be called from the script, which means that the class is more flexible in the long run? A question of encapsulation vs modularity, it would seem. Your thoughts are gratefully accepted. Erik Erik Price Web Developer Temp Media Lab, H.H. Brown [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php