PHP Design Patterns .

PHP Design Patterns Non OO MVC

About the Non-OO MVC

In the MVC (Model-View-Controller) pattern, functionality is broken into three sections.

The Controller controlls all processing, including determing what if anything was input, determing what model and view functions to call, and handling passing data to and from the model and view functions.

The Model controlls all data access and manipulation, such as database io and any calculations.

The View controlls all output formatting, such as creating an html page.

While MVC works extremely well with Object Oriented Programming, it works equally well in procedural programming as this example shows.

Simple-Non-OO-MVC-Controller.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

include_once('Simple-Non-OO-MVC-Model.php'); include_once('Simple-Non-OO-MVC-View.php'); $page = getPage(); if ("TITLE" == $page) { $title = getTitle(); showTitlePage($title); } elseif ("AUTHOR" == $page) { $author = getAuthor(); showAuthorPage($author); } function getPage() { if (isset($_GET["page"])) { $pageIn = $_GET["page"]; switch($pageIn) { case "title": $page = "TITLE"; break; case "author": $page = "AUTHOR"; break; default: $page = "TITLE"; break; } } else { $page = "TITLE"; } return $page; }
To download source right-click here and "Save As...".

Simple-Non-OO-MVC-Model.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

function getAuthor() {return 'Larry Truett';} function getTitle() {return 'PHP for Cats';}
To download source right-click here and "Save As...".

Simple-Non-OO-MVC-View.php

//copyright Lawrence Truett and FluffyCat.com 2006, all rights reserved

function showAuthorPage($authorIn) { $page = htmlBegin() . 'AUTHOR: '.$authorIn.htmlEnd(); echo $page; } function showTitlePage($titleIn) { $page = htmlBegin() . 'TITLE: '.$titleIn.htmlEnd(); echo $page; } //breaking up the quotes only so this example will display function htmlBegin() { return '<'.'HTML><'.'HEAD><'.'TITLE><'.'/TITLE><'.'/HEAD><'.'BODY>'; } function htmlEnd() { return '<'.'/BODY><'.'/HTML>'; }
To download source right-click here and "Save As...".

Test the Non-MVC-Controller

test with no paramaters
test with a paramater of page=author
test with a paramater of page=title
test with a paramater of page=nonsense

References


PHP
The Official PHP web site
Comments Comments are left by visitors to FluffyCat.com and may or may not be accurate.
Comment by BDKR on 2014-01-31 Rate this Comment

It's cool to see this. I thought I was the only one that realized that the pattern exists regardless of the paradigm used to implement it.

Comment by Larry on 2009-03-29 Rate this Comment

I'm sure this will cause MVC OO fans to wince, but some COBOL systems I worked on in the 1980s (which were designed in the 1970s) had an excellent MVC implementation. The controller module, often called the controller module, would generally begin by determining what was being done, call the appropriate database io modules, call calculation modules, and then lastly call the screen formatting modules.

 
Sign in to comment on PHP Design Patterns Non OO MVC.