How many times have you sat down to code a script and - halfway through - thought to yourself, "Didn't I do something similar just last week?"
If you're anything like the average Web developer, you probably ask yourself this question at least once every few days. And more often than not, you're torn between coding the same functions again (because you're already halfway there and looking for last week's code just isn't worth the effort) and spending an hour searching for that itty-bitty script on your twenty-terabyte hard drive (because it's just more convenient to modify last week's code than to write it all over again.)
It's to resolve precisely this sort of dilemma that a bunch of white-haired software gurus (who, according to legend, live on a snowy mountain peak in the Himalayas and spend most of their time coding algorithms to calculate the value of pi to the nth decimal) came up with the concept of object-oriented programming. Very simply, object-oriented programming allows developers to create reusable, extensible program modules in order to speed up code development and maintenance.
Now, you may not know this, but my favourite language and yours, PHP, comes with some pretty powerful OOP capabilities. And over the course of this article, I'm going to demonstrate some of them by building an object to address a very common task - generating a menu tree on a Web site. That's not all, though - once I've successfully created a Menu object, I'm going to torture-test it with some of the most popular menu systems available on the Web to see if it does, in fact, offer any significant advantages.
If all goes well, this experiment should teach you a little about the theory and possible applications of OOP; provide you with a Menu class which is (hopefully) useful to you in your development activities; and perhaps even spark off some ideas for using PHP classes in your next project. If, on the other hand, I crash and burn, you'll have something to snicker over at the pub tonight.