PHP is a nice, easy entry level embedded tagging system - an "HTML++" language in which programmers and web site developers can drop elements of code into a web page and perform simple and straigtforward tasks very quickly. But the downside of that is that the resultant file very quickly becomes a confusing mixture of HTML, program logic that's associated with the application's data, and programming logic that's associated with the formatting of that data for web display. As a control case, I've coded an example showing those issues
[here].
By splitting the code into a series of areas, we can make the whole thing much more followable and easy to debug, even though it will be longer. The areas are:
• the VIEW - the HTML itself, with very limited markup to indicate where data is to be inserted
• the MODEL - the PHP code that deals purely with the data and calculations and logic thereon
• the FRAMEWORK - surrounding code that starts (bookstraps) the whole processing of the page
• HELPERS - common routines that may come standard with the framework, or may be ones you write and
• the CONTROLLER - the PHP code that ties all the other elements together
You'll see all of these elements of code
[here] - that's a conversion of the example just above into a single file in which each of them is in its own section of the file.
But a view may want to be shared by a number of controllers, and the same model used in several applications. Framework code will be shared widely, and there will be a commonality in helpers - and so you'll want to take each of these elements out into a separate file for separate maintainance, and for sharing on a different pattern. The result is a file that contains just the controller, and which references the other files. There's an example
[here] ... the code looks like this:
# load in the framework and framework components, and bootstrap the application
include ("z_framework.php");
# load in the user's helpers
include ("z_helpers.php");
# load in the user's model classes
include ("z_model.php");
// Run model code
$results = getstats("Manchester"); // Running model code
// Feed results into view-populating object
$fill["year"] = date("Y");
$table = starttable(); // using helpers
foreach (array_keys($results) as $paramName) {
$table .= startrow();
$table .= cell($paramName);
$table .= cell($results[$paramName]);
$table .= endrow();
}
$fill["table"] = $table . endtable();
// Populate and render the view
sendToOutput($fill,"z_view.htp");
// Using the Framework to render the view
And each of the other files is straightforward:
The framework and components
[here]
The helpers
[here]
The model code
[here]
and the view template
[here]
This example was written during a private PHP course over the last few days, and will now be used on some of our
public PHP courses to help people understand the why and how of "MVC" and Frameworks.
(written 2013-04-19, updated 2013-04-20)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Q915 - Object Orientation and General technical topics - Principles of Model - View - Controller [687] Presentation, Business and Persistence layers in Perl and PHP - (2006-04-17)
[2199] Improving the structure of your early PHP programs - (2009-05-25)
[2612] The Model, View, Controller architecture (MVC) - what, why and how. - (2010-02-01)
[3237] Using functions to keep look and feel apart from calculations - simple C example - (2011-04-09)
[3454] Your PHP website - how to factor and refactor to reduce growing pains - (2011-09-24)
[3624] Why do we need a Model, View, Controller architecture? - (2012-02-25)
[3705] Django Training Courses - UK - (2012-04-23)
[3919] What is a web framework? - (2012-11-10)
[4010] Really Simple Rails - (2013-02-17)
[4114] Teaching CodeIgniter - MVC and PHP - (2013-06-12)
[4320] An example of Model-View-Controller techniques in a Perl / CGI script - (2014-11-20)
[4391] Refactoring Perl applications to give them a rosy future - (2015-01-11)
[4527] Hello Flask world / Python web micro framework - (2015-10-11)
[4641] Using an MVC structure - even without a formal framework - (2016-02-07)
[4691] Real life PHP application using our course training MVC example - (2016-06-05)
H302 - PHP - MVC, 4 layer model and templating [1634] Kiss and Book - (2008-05-07)
[1716] Larger applications in PHP - (2008-07-22)
[1766] Diagrams to show you how - Tomcat, Java, PHP - (2008-08-22)
[2174] Application design in PHP - multiple step processes - (2009-05-11)
[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)
[4314] PHP training - refreshed modern course, backed up by years of practical experience - (2014-11-16)
Some other Articles
Passing variable between PHP pages - hidden fields, cookies and sessionsEven early on, separate out your program from your HTML!Arrays in PHP - contain different and even mixed data typesThe woman, the television, the bullock and DarlingtonMVC and Frameworks - a lesson from first principles in PHPHandling requests to a forum - the background processApache httpd - a robust, open source web serverBackups by crossover between network centres - setting up automatic scp transfersSessions, forms and validation in CodeIgniter - early examplesSeamless, integrated IT - we have a long way to go!