Application design in PHP - multiple step processes
Archive - Originally posted on "The Horse's Mouth" - 2009-05-11 06:31:54 - Graham Ellis
When you're putting together a multiple step process in PHP (and that could be anything from a forum to an ordering system), you should bare in mind that each of the program elements will be the 'intermediate' step that takes you from one display to the next ... and that you can't be sure when page "X" has been completed that the next page to be presented really WILL be page "Y" - after all, if the user has failed to enter his password correctly on page "X" you'll want to redisplay it ...
... so here, in principle, is how your code should look. Your application will pick up form data and any existing information from the user's session, then "finish" processing from those inputs. Part of that finishing process will be making the decision as to what page to move onto next, so after finishing from page "X" the code will prepare for page "Y", or page "X" again perhaps. Upon completion of those preparations, the (newly updated) session is saved away, and a template for the new page completed.
Rather than store all the code associated with such a multipage application in one file / logical area, we'll usually split it up. The top level code goes into one file, the "business logic" - code to do the actual application work - into another. A third file contains the "web helpers" - routines which handle the forms, sticky fields, web principles, etc. And a fourth file contains a template - what the web page presented will actually look like.By splitting the code in this way, it becomes easier to maintain as it's in sensible sized chunks. It also allows for elements to be reused in other areas of the web site / application - for example, a template might be shared right across a site, web helpers across many sites, and the business logic may be used offline as well as on the web. Finally, the division principle allows each section to be independently looked after by experts in that section - you need staff who are masters at each individual trade rather than staff who can dabble a bit in all areas.
If you have heard about MVC ("Model View Controller") parts of what I have written may sound very familiar indeed ... and that is no accident!
We have a sample 'shell' of an application that goes through a multistep process using these principle and you can run it here. Source code is here (top level), here (business logic), here (web helpers) and here (template).
This subject is taught briefly on our learning to program in PHP and PHP programming courses, and much more fully covered during our PHP techniques workshop