Your Web application includes both presentation and logic, and that logic is further divided into business and display logic. How are you going to manage these various layers? How about keeping each of the logically separate areas in a separate file so that the look and feel can be maintained quite separate from the operations?
Here's a sample application written using this form of separation:
INDEX.PHP4 - THE TOP LAYER
<?
# Separating display, business logic and helpers
include "business.inc";
include "helpers.inc";
include "display.inc";
?>
DISPLAY.INC - THE PRESENTATION LAYER
<html>
<head><title><? title(); ?></title></head>
<body bgcolor=#CCCCFF><h1>Separating the logic</h1>
This is a demonstration of the separation of the "business logic"
from the main code on a PHP page. It's all transparent to the visitor
to your web site, but the page at the actual location called up is simply
a few includes and the real work is is in those:<ul><li>Business Logic
<li>Display Helper logic<li>Display template</ul>
<center><i>Using PHP as it's really intended ;-)</i></center><br><br>
<? result() ; ?>
<br><br>
<? question() ; ?> ...<p>
<form><table>
<tr><td>Let's have you enter an age:</td><td><? input_box("age",4); ?></td></tr>
<tr><td>and a gender</td><td><? input_radio("gender","Male|Female"); ?></td></tr>
<tr><td> and ... </td><td><input type=submit></td></tr>
</table>
</form><br><br>
<? copyright() ; ?>
</body>
</html>
HELPERS.INC - THE HELPERS LAYER
<?
function title() {
global $title;
echo $title;
}
function question() {
global $formheader;
echo $formheader;
}
function copyright() {
echo "Copyright, Well House Consultants, ".date("Y");
}
function result () {
global $result;
echo "** <u><b>$result</b></u> ** ";
}
function input_radio($name,$options) {
foreach (explode("|",$options) as $option) {
echo "<input type=radio name=$name value=$option> $option<br>";
}
}
function input_box($name, $size) {
echo "<input name=$name size=$size>";
}
?>
BUSINESS.INC - THE BUSINESS LOGIC LEVEL
<?
$resolved = ($_GET[age] != "");
if ($_GET[age] > 19) {
$describe = "Adult";
if ($_GET[gender] == "Male") $describe = "Gentleman";
if ($_GET[gender] == "Female") $describe = "Lady";
} elseif ($_GET[age] > 12) {
$describe = "Teenager";
if ($_GET[gender] == "Male") $describe = "Young Gentleman";
if ($_GET[gender] == "Female") $describe = "Young Lady";
} elseif ($_GET[age] > 1) {
$describe = "Child";
if ($_GET[gender] == "Male") $describe = "Boy";
if ($_GET[gender] == "Female") $describe = "Girl";
} else {
$describe = "Baby";
}
if ($resolved) {
$title = "About our person ....";
$formheader = "Please enter details for another person";
$result = "A $_GET[age] year old $_GET[gender] would be described as a '$describe'";
} else {
$title = "A Welcome page";
$formheader = "Please enter some information about a person";
$result = "Your results will appear here";
}
?>
See also
PHP Best Practice - Module
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via
our main web site
PHP - MVC, 4 layer model and templating [1634] - ()
[1716] - ()
[1766] - ()
[2174] - ()
[2199] - ()
[2221] - ()
[3454] - ()
[3539] - ()
[3956] - ()
[4066] - ()
[4114] - ()
[4314] - ()
Designing PHP-Based Solutions: Best Practice [123] - ()
[237] - ()
[261] - ()
[340] - ()
[394] - ()
[426] - ()
[563] - ()
[572] - ()
[839] - ()
[896] - ()
[936] - ()
[945] - ()
[1047] - ()
[1052] - ()
[1166] - ()
[1181] - ()
[1182] - ()
[1194] - ()
[1321] - ()
[1323] - ()
[1381] - ()
[1389] - ()
[1390] - ()
[1391] - ()
[1482] - ()
[1487] - ()
[1490] - ()
[1533] - ()
[1623] - ()
[1694] - ()
[1794] - ()
[2199] - ()
[2221] - ()
[2430] - ()
[2679] - ()
[3539] - ()
[3813] - ()
[3820] - ()
[3926] - ()
[4069] - ()
[4118] - ()
[4326] - ()
[4641] - ()
[4691] - ()
Web site techniques, utility and visibility [23] - ()
[32] - ()
[98] - ()
[109] - ()
[117] - ()
[142] - ()
[165] - ()
[173] - ()
[179] - ()
[182] - ()
[197] - ()
[202] - ()
[222] - ()
[259] - ()
[261] - ()
[268] - ()
[274] - ()
[276] - ()
[278] - ()
[284] - ()
[288] - ()
[311] - ()
[314] - ()
[320] - ()
[322] - ()
[347] - ()
[348] - ()
[369] - ()
[376] - ()
[414] - ()
[492] - ()
[510] - ()
[528] - ()
[533] - ()
[649] - ()
[658] - ()
[681] - ()
[718] - ()
[732] - ()
[757] - ()
[767] - ()
[800] - ()
[893] - ()
[916] - ()
[976] - ()
[994] - ()
[1015] - ()
[1029] - ()
[1055] - ()
[1104] - ()
[1177] - ()
[1184] - ()
[1186] - ()
[1198] - ()
[1207] - ()
[1212] - ()
[1237] - ()
[1297] - ()
[1437] - ()
[1494] - ()
[1505] - ()
[1506] - ()
[1513] - ()
[1534] - ()
[1541] - ()
[1554] - ()
[1610] - ()
[1630] - ()
[1634] - ()
[1653] - ()
[1711] - ()
[1747] - ()
[1756] - ()
[1793] - ()
[1797] - ()
[1833] - ()
[1856] - ()
[1888] - ()
[1955] - ()
[1961] - ()
[1970] - ()
[1982] - ()
[2056] - ()
[2065] - ()
[2225] - ()
[2332] - ()
[2333] - ()
[2334] - ()
[2335] - ()
[2336] - ()
[2337] - ()
[2338] - ()
[2339] - ()
[2340] - ()
[2340] - ()
[2341] - ()
[2389] - ()
[2410] - ()
[2519] - ()
[2532] - ()
[2552] - ()
[2569] - ()
[2668] - ()
[2981] - ()
[3022] - ()
[3087] - ()
[3149] - ()
[3197] - ()
[3367] - ()
[3426] - ()
[3491] - ()
[3532] - ()
[3554] - ()
[3563] - ()
[3589] - ()
[3623] - ()
[3734] - ()
[3744] - ()
[3745] - ()
[3776] - ()
[3896] - ()
[3974] - ()
[4001] - ()
[4076] - ()
[4115] - ()
[4136] - ()
[4239] - ()
[4376] - ()
[4401] - ()
[4474] - ()
[4492] - ()
resource index - PHP
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.