« moving from stunnel to private network | Main | Network Computing Weblog »

January 14, 2003

Object Oriented PHP

Many years ago I made my entrance into the web scripting world by using PHP on a very small site. It was a typical configuration, PHP with MySQL enabled to grab information about products for sale. It didn't take long for me to realize that the pages were static enough to be pre-generated, with a token amount of PHP to track session information.

Since everything was already written in PHP, I created an admin suite that used PHP to pull from the database and write out a complete set of PHP files, which were primarily static HTML. Big gain in speed (I was running this on a server which hosted 800 other sites).

Recently the site broke into four different sub sites and I had the challenge of modifying my code to create four different sets of pages. At first this was easy, I created a site table and looped over each site, using data from the site table to plug in things like name, meta tags, path etc.

It was more of a challenge when the owner wanted to tweak specific pieces of functionality on individual sites. I needed a default template which would create PHP/HTML pages as they currently looked and behaved, but have a way for any piece of that page to be overridden.

Having experience with XML and XSLT, I was tempted to rewrite everything. But the amout of work to convert the existing output into a defined XML format and then create stylesheets for the different pages was more than I wanted to commit to (and more than the company wanted to spend).

I also looked at PHP's FastTemplate. I could have gotten some mileage out of it, but the difference between pages was significantly greater than structure of HTML (ie. pull list of products from the database and in one instance create a dropdown with a form action to the url and in another instance create a column of hrefs on a different part of the page). Some might argue that FastTemplate was the answer, it didn's seem quite right to me.

Enter Object Orient PHP. With all my current output being generated by PHP scripts, and many times via functions, I decided that OOPHP would most easily get me to where I needed to be. OOPHP seems primitive, but it had everything I needed. I created a class filled with functions that would output the PHP/HTML for each piece of my default page. Then I created a class that extended my primary class and replaced certain functions with completely different functionality and layout.

The result, a simple method for using inheritance to generate a number of pages with some similar chunks and other parts completely different.

Posted by mike at January 14, 2003 2:02 PM