The Good Old Days

Before the release of PHP 4, the language didn't embrace the Object-Oriented paradigm. Back then, the usual way of writing applications was by using procedures and global state. Concepts like Separation of Concerns (SoC) and Model-View-Controller (MVC) were alien among the PHP community. The example below is an application written in this traditional way, where applications were composed of many front controllers mixed with HTML code. During this time, Infrastructure-, Presentation-, UI-, and Domain-layer code were all tangled together:

include __DIR__ . '/bootstrap.php';

$link = mysql_connect('localhost', 'a_username', '4_p4ssw0rd');

if (!$link) {
die('Could not connect: ' . mysql_error());
}

mysql_set_charset('utf8', $link);
mysql_select_db('my_database', $link);

$errormsg = null ;
if (isset($_POST['submit'] && isValid($_POST['post'])) {
$post = getFrom($_POST['post']);
mysql_query('START TRANSACTION', $link);
$sql = sprintf(
"INSERT INTO posts (title, content) VALUES ('%s','%s')",
mysql_real_escape_string($post['title']),
mysql_real_escape_string($post['content']
));

$result = mysql_query($sql, $link);
if ($result) {
mysql_query('COMMIT', $link);
} else {
mysql_query('ROLLBACK', $link);
$errormsg = 'Post could not be created! :(';
}
}

$result = mysql_query('SELECT id, title, content FROM posts', $link);
?>
<html>
<head></head>
<body>
<?php if (null !== $errormsg) : ?>
<div class="alert error"><?php echo $errormsg; ?></div>
<?php else: ?>
<div class="alert success">
Bravo! Post was created successfully!
</div>
<?php endif; ?>
<table>
<thead><tr><th>ID</th><th>TITLE</th>
<th>ACTIONS</th></tr></thead>
<tbody>
<?php while($post = mysql_fetch_assoc($result)) : ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['title']; ?></td>
<td><?php editPostUrl($post['id']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
</html>
<?php mysql_close($link); ?>

This style of coding is often referred to as the Big Ball of Mud we mentioned in the first chapter. An improvement seen in this style, however, was to encapsulate the header and the footer of the webpage in their own separate files, which were included in the header and footer files. This avoided duplication and favored reuse:

include __DIR__ . '/bootstrap.php';

$link = mysql_connect('localhost', 'a_username', '4_p4ssw0rd');

if (!$link) {
die('Could not connect: ' . mysql_error());
}

mysql_set_charset('utf8', $link);
mysql_select_db('my_database', $link);

$errormsg = null;

if (isset($_POST['submit'] && isValid($_POST['post'])) {
$post = getFrom($_POST['post']);
mysql_query('START TRANSACTION', $link);
$sql = sprintf(
"INSERT INTO posts(title, content) VALUES('%s','%s')",
mysql_real_escape_string($post['title']),
mysql_real_escape_string($post['content'])
);

$result = mysql_query($sql, $link);
if ($result) {
mysql_query('COMMIT', $link);
} else {
mysql_query('ROLLBACK', $link);
$errormsg = 'Post could not be created! :(';
}
}

$result = mysql_query('SELECT id, title, content FROM posts', $link);
?>
<?php include __DIR__ . '/header.php'; ?>
<?php if (null !== $errormsg) : ?>
<div class="alert error"><?php echo $errormsg; ?></div>
<?php else: ?>
<div class="alert success">
Bravo! Post was created successfully!
</div>
<?php endif; ?>
<table>
<thead>
<tr>
<th>ID</th>
<th>TITLE</th>
<th>ACTIONS</th>
</tr>
</thead>
<tbody>
<?php while($post = mysql_fetch_assoc($result)): ?>
<tr>
<td><?php echo $post['id']; ?></td>
<td><?php echo $post['title']; ?></td>
<td><?php editPostUrl($post['id']); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
<?php include __DIR__ . '/footer.php'; ?>

Nowadays, and although it is highly discouraged, there are still applications that use this procedural way of coding. The main disadvantage of this style of architecture is that there's no real Separation of Concerns — the maintenance and cost of evolving an application being developed this way increases drastically in relation to other well-known and proven architectures.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset