Efficient PHP applications - framework and example
Archive - Originally posted on "The Horse's Mouth" - 2007-12-28 20:09:20 - Graham EllisThere are many elements to your on line application - there's the "look" (1), the "feel" (2), the algorithms behind it (3), and the way the whole thing is bolted together (4).
1. The look may be shared across all the pages of your web site.
2. The feel you choose to use may be shared across all your online applications, perhaps numerous web sites, but not be applicable to "flat" pages.
3. The algorithms are likely to be shared by a selection of applications, some of which may run in a batch (crontab) type environment that don't share the look and feel.
4. The controlling logic that ties the whole application together will be unique, but will aways be based on the following steps as it moves from one page to the next:
• read in any "session" or "shopping cart" information
• read and validate form fields from the form JUST COMPLETED and in the process work out which page is to be displayed next
• perform any general calculation and logging tasks needed for each page processed
• prepare for the next page / form that's to be displayed
• save away and shopping cart / session data that's to be retained
• send out the next page.
A couple of years ago, I had the pleasure of spending a little time listening to Rasmus Lerdorf - the author of PHP - as he gave an advanced tutorial session; he was talking of a similar 4 layer model to my description at the beginning of this piece, and I have used similar for numerous applications since - indeed I have my own standard "feel" file that's used pretty much all over the place. It's wonderful in that I can employ techniques such as sticky fields, and do all my handling of characters such as &, <, ", ' and > in a single place which has been fully tested and should be fairly bullet proof.
I awoke early on Christmas morn. Perhaps you did too? The sound of reindeer in the distance as Santa rode past from one good child's house to the next? No - it was the rain against the window and a feeling that I really ought to provide a good, updated example of the four layer model. And I got the inspiration!
The Top Layer - in which the whole is tied together - is a PHP file in which you can see all the steps laid down can be seen (source code) here. This is element "4" in my description.
The Business Logic - also in PHP contains the meat of the calculations and application-specific validation. In a demonstration, there's very little to this, but you can see the code here - element 3 in my description.
The Web Helpers - again in PHP - are the functions that provide form support, sticky fields, secure handling of nasty characters - element 2 in my description and available for my demonstration here in source code.
The Feel of the page - written in HTML, but with Javascript, CSS and other bits too - is the final element. It's in the form of a template where I have used placeholders of the form '%[[:alnum:]]%' [Regular expression] to signify where the template is to be completed by the web helpers, using results generated by the business logic in an order controlled by the top layer. See the template here.
I have that code running on our web site - link here - so that you can see how it works.
But this code is - just a demonstration; the power comes in the re-use of the formula. Come the 26th, I was testing a new incident book system for our internal use for reporting anything from scuff marks that need repainting, to making notes of special dietary requirements or letting everyone know that we're running out of Moroccan Chicken. Really was written rather quickly. And you're welcome to look through the public records too ... see a practical (in terms of use) and practical (in terms or writing speed) and practical (in terms of maintainability) system too. Perhaps I should wait until later in the year to show you a secure booking system? I know I've only got a couple of days, but that's plenty isn't it?