Main Content

Larger applications in PHP

Archive - Originally posted on "The Horse's Mouth" - 2008-07-22 17:34:04 - Graham Ellis

I've just finished a two day PHP techniques course - a subject that's tremendous fun to present, and a huge benefit to the delegates, as PHP programs can be written with great beauty, maintainability and expandability (like a Picasso) or they can be an unmaintainable mess (making the dog's dinner look neat!) and this course takes people who already have some PHP experience and helps them learn how to produce Picassos and not dog's dinners!

When you first write a larger application in PHP, it's tempting to provide a separate URL and file for each stage of the application ... but you quickly learn that's NOT the way to go when you discover that program section "x" has to be able to produce form "y" "z" and "a" depending on what the user entered ... and that users will insist on bookmarking intermediate URLs and will appear, Hogwarts wizard like, in mid application if you give them half a chance. Separate files for each stage of the main application is NOT the way to go - you want the whole under a single umbrella!

Our classic "4 layer model" example - (source code, run it) provides a series of stages to an application all under a single parasol - with each step of the process being controlled by a switch statement - the first finishing up from data entered on the previous page, and the second preparing for the next page, whichever one that might be as it depends on what the user had entered.

The four layer model is neat - very neat (and I don't claim to have originated - I give full credit to Rasmus Lerdorf who wrote PHP, and from whom I learned of the technique. But it has always worried me slightly that - as I implement it - ALL of the business logic has been loaded for every page, and that can get a little inefficient as the application moves up the scale to massive.

A chance comment today from a delegate - and I have developed the application forward one more stage. By replacing each switch statement with an include to bring in only the correct finishing code then only the correct preparation code, the technique becomes massively extensible without the need for the main code section to grow either. One of those "bolt of lightening moments" if you like.

Here's the heart of the code ...

// Deal with initialisation
if (! $_SESSION[stage] ) $_SESSION[stage] = 0;
$stage = $_SESSION[stage];
// Finish from the previous stage and prepare for next
// These files would usually be in another directory!
// and one that did NOT have its own URL!
// It would also be a good idea to docement each stage here!
include ("stages.finish$stage.inc");
include ("stages.prepare$stage.inc");


http://www.wellho.net/demo/stages.php

You can try it our here. The sample has got sticky fields, data validation, error messages and all the rest in it - and it uses a separate template to keep the look and feel apart from the business logic. The web specific helper routines are also kept in a separate file so they can be shared by other applications. (Am I starting to sound like an advert?)

Source code:
Top level
Web Helpers
Look and feel template
Finish code -from initialise, from user entering his name and department and from each main data entry.
Preparing for next page - for reading name and department, for reading main data entries and for confirming logout.

If you're looking to learn PHP and you feel this is the sort of thing for you, we're running our PHP techniques course every 2 months. If you feel it's likely to be too advanced for you at first, why not come on our PHP Programming course which runs every other month - next start date, 11th August!