I learned C++ in the early 1990s and started my jour­ney on the Object Orient Programming road although for like 15 years, that road was only a block long. Even still, it began a inter­nal bat­tle with myself that was just sim­mer­ing but now has started to boil over.

I started using php start­ing with ver­sion 3 circa 1999 although I really didn’t do too much with it until ver­sion 4 came out in 2000. Over the years I have devel­oped my own frame­work for doing all my web­sites and have built what I call the “site man­ager”, a CMS of sorts for cus­tom web­sites. It isn’t a “real” CMS like Joomla, Drupal, or WordPress since it doesn’t have a lot of reader feed­back capa­bil­i­ties. On the other hand, I have been able to use the code for quite diver­gent sites in use and look.

Anyhow, I am rewrit­ing my site man­ager (ver­sion 4) using OOP tech­niques for PHP 5. This is a seri­ous rewrite in that a lot of new code is being writ­ten to replace old code, unlike the pre­vi­ous 2 ver­sions which sim­ply added to or mod­i­fied exist­ing code. The old code was pri­mar­ily “pro­ce­dural” code with only a small bit of OOP code inter­spersed, often from using PEAR classes such as DB/MDB2. Now I am attempt­ing to be as com­plete as I can with OOP in my code as PHP allows. And this is where I keep run­ning into my ever present con­flict with OOP pro­gram­ming in gen­eral. I keep find­ing I can sim­ply copy my pro­ce­dural code (nor­mally a col­lec­tion of func­tions) straight into a class, make a cou­ple edits such as adding $this-> to a lot of method calls and bam, I have a whole new class that was just a col­lec­tion of func­tions gath­ered into a sin­gle file. Now, I am guess­ing that I have writ­ten 50% new code sim­ply because it was eas­ier than try­ing to con­vert the old, but in gen­eral, a lot of my old pro­ce­dural code is find­ing it way into my OOP ( I am hav­ing a ter­ri­ble time not want­ing to make a ref­er­ence to Reeses, so there it is, and now I am hungry ).

SIDENOTE: As nice as the PEAR classes are, and would have reduced the amount of rewrite I would have needed, I ran into a prob­lem where a web­site I did for a cus­tomer sud­denly stopped work­ing because a PEAR class was updated on the server and appar­ently some­thing changed. I stopped trust­ing 3rd party code and only use my own. I know, silly me. I do know how to include PEAR libraries in my own stuff so that kind of thing wouldn’t hap­pen (and is what I started to do), but I guess I am just a con­trol junkie and have a ten­dency to want to use my own code for most things.

OOP vs Procedural cod­ing issues (and remem­ber, I have been play­ing with OOP since the early 1990s), have always con­fused me in one way or another. Simply put, OOP to me is just a fancy way of doing pro­ce­dural code… I know, a gag­gle of OOP pro­gramers are on their way to Omaha to skin me alive for say­ing that. Wait a sec.

Let me explain. In my way of think­ing, we as humans are pro­ce­dural. We start here and end there. OOP con­cedes this need of time based actions with its inclu­sion of meth­ods (func­tions for those of you who pre­fer that term). Methods are almost always pro­ce­dural code in some way, if not always. Even the com­mon nam­ing of cer­tain types of meth­ods, namely, set­Some­thing and get­Somthing estab­lish pro­ce­dures whereby we change the state of a prop­erty of an object. Java goes so far as requir­ing the main method, which estab­lishes the para­me­ters for what the object is and does. It is the one pro­ce­dure that rules them all.

To say it sim­ply, an object with­out time based actions is worth­less. And time based actions are pro­ce­dural, they start at some point, do things, and even­tu­ally end.

Ok, I can hear some OOP pro­gram­mers say­ing, “You just don’t get it.” And I think that is what I am say­ing in part but I am also say­ing I am being dri­ven crazy by those who insist that OOP has no pro­ce­dural code since to me, it does. It just does pro­ce­dural code much more ele­gantly. It pro­vides a struc­ture and dare I say it, the proper pro­ce­dures (ok, for you silly guys, well estab­lished Patterns) to write code that can then be used over again, expanded upon, and in gen­eral just work.

But I can also hear pro­ce­dural pro­gram­mers say­ing, “Why in the world would you then want to switch to that crazy world called OOP, its a waste of time and ‘every­one’ knows that OOP code runs slower espe­cially with PHP?” Ok, that is an easy answer, there are a lot of good con­cepts in OOP that should be used, such as inher­i­tance, reuse, encap­su­la­tion etc. OOP makes these con­cepts a real­ity. For you PHP pro­gram­mers, check out the book Object-Oriented PHP by Peter Lavin. That book was the final straw that got me to start this crazy project of tak­ing a per­fectly work­ing code base and turn it into OOP based code. The book is short but to the point and may encour­age the rest of you hold outs to really inves­ti­gate OOP finally in PHP.

One last thing, and maybe this will mol­lify some OOP peo­ple out there, I refused to do any OOP in PHP 4 since it was a lame excuse for OOP and didn’t fol­low very well sev­eral of the impor­tant con­cepts that I believe makes OOP worth the has­sle. Yes, it took me a long time to decide to finally move all my code over to OOP even though I have been using PHP5 in pro­duc­tion prac­ti­cally since the day it came out of beta (almost 4 years ago! good grief you guys still writ­ing in PHP4, give it up!). I just didn’t have the time to con­vert the code since I was too busy using it to cre­ate new web­sites. BTW: the sim­ple fact that I could reuse the same pro­ce­dural code for many web­sites with com­pletely diver­gent needs speaks to the fact that although I was writ­ing pure pro­ce­dural code, I was in my own way, fol­low­ing some of the OOP con­cepts includ­ing encap­su­la­tion, reuse etc. and it has greatly made the con­ver­sion to OOP easier.

So, although I am neck deep into OOP now, I still keep ask­ing the silly ques­tion that tor­ments my pro­gram­ming soul, “Isn’t this really just fancy pro­ce­dural programming?”

Tagged with:
 

Leave a Reply

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...