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!
(written 2008-07-22, updated 2008-07-23)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
H310 - PHP - Putting it all together [468] Stand alone PHP programs - (2005-10-18)
[687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
[1754] Upgrade from PHP 4 to PHP 5 - the TRY issue - (2008-08-15)
[1794] Refactoring - a PHP demo becomes a production page - (2008-09-12)
[1840] Validating Credit Card Numbers - (2008-10-14)
[1962] Index Card System for Game Characters in PHP - (2008-12-27)
[2275] Debugging multipage (session based) PHP applications - (2009-07-09)
[2635] A PHP example that lets your users edit content without HTML knowledge - (2010-02-14)
[2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
[3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
H302 - PHP - MVC, 4 layer model and templating [1634] Kiss and Book - (2008-05-07)
[1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
[2174] Application design in PHP - multiple step processes - (2009-05-11)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2221] Adding a newsfeed for your users to a multipage PHP application - (2009-06-06)
[3539] Separating program and artwork in PHP - easier maintainance, and better for the user - (2011-12-05)
[3956] Zend / layout of MVC and other files in an example application (PHP) - (2012-12-16)
[4066] MVC and Frameworks - a lesson from first principles in PHP - (2013-04-19)
[4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
[4314] PHP training - refreshed modern course, backed up by years of practical experience - (2014-11-16)
Some other Articles
Some Ruby lesser used functionsA special day - last Friday in JulyIncreasing Java Virtual Machine memory for TomcatQ - Should I use Perl or Python?Larger applications in PHPPictures from South HampshireHillier Gardens, HampshireWays to accept credit cards - or not!As different as night and tyresRapid growth leads to server move