Hi,
My take on this is that a script written in a procedural style will execute faster than a script written with an OO style 99% of the time. However a programmer should be able to write many more scripts using OO than writing Procedural in the same amount of time.
An example:
This is how I would write a script to display a list of articles using a procedural style.
PHP Code:
<?php
if (!$conn = @mysql_connect('localhost', '', '')) {
trigger_error('Invalid database connection settings');
} elseif (!mysql_select_db('article', $conn)) {
trigger_error('Invalid database name');
}
$sql = 'SELECT id, title, content, author, date FROM article';
if (!$resource = @mysql_query($sql, $conn)) {
trigger_error('Invalid query : ' . $sql);
}
?>
<html>
<head>
<title>Article List</title>
</head>
<body>
<h1>Article List</h1>
<?php if (mysql_num_rows($resource) == 0) { ?>
<p>There are no articles.</p>
<?php } else { ?>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Date</th>
<th>Action</th>
</tr>
<?php while ($record = mysql_fetch_array($resource, MYSQL_ASSOC)) {?>
<tr>
<td><?php echo $record['title']; ?></td>
<td><?php echo $record['author']; ?></td>
<td><?php echo $record['date']; ?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
</body>
</html>
Fairly basic, no surprises. Couldnt really get this run much faster.
Now if I add a data access layer, I dont have to worry about mysql connections and errors on every page. I can also change the database without going through every page on my site and changing every line with a mysql function.
PHP Code:
<?php
require_once('MySqlDataAccess.php');
$dbConn = &new MySqlConnection('localhost', 'article', '', '');
$sql = 'SELECT id, title, content, author, date FROM article';
$recordSet = $dbConn->executeQuery($sql);
?>
<html>
<head>
<title>Artile List</title>
</head>
<body>
<h1>Article List</h1>
<?php if ($recordSet->size() == 0) { ?>
<p>There are no articles.</p>
<?php } else { ?>
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>Date</th>
</tr>
<?php while($recordSet->next()) { ?>
<tr>
<td><?php echo $recordSet->get('title'); ?></td>
<td><?php echo $recordSet->get('author'); ?></td>
<td><?php echo $recordSet->get('date'); ?></td>
</tr>
<?php } ?>
</table>
<?php } ?>
</body>
</html>
This script is now takes marginally longer to execute however, I can write it marginally faster. The time saved adds up over the amount of pages in the application.
Ive noticed that I use the same sql query to get the article list on many pages. So I add a Data Source layer, and I can call a function on an object each time I need the list of articles.
I replace this code:
PHP Code:
require_once('MySqlDataAccess.php');
$dbConn = &new MySqlConnection();
$sql = 'SELECT id, title, content, author, date FROM article';
$recordSet = $dbConn->executeQuery($sql);
with this code:
PHP Code:
require_once('ArticleTableDataGateway.php');
$articleTDG = &new ArticleTableDataGateway();
$recordSet = $articleTDG->findAll();
The page is now marginally slower to execute.
Now I notice that each page of my site uses the same structure (dtd, head, meta tags, nav etc) for the html page. So I decide to add a template structure/engine.
The code now looks like this:
PHP Code:
<?php
require_once('ArticleTableDataGateway.php');
$articleTDG = &new ArticleTableDataGateway();
$recordSet = $articleTDG->findAll();
$tpl = new Template('main.tpl', 'article_list.tpl');
$tpl->set('title', 'Article List');
$tpl->set('recordSet', $recordSet);
$tpl->render();
?>
Ill leave the templates to the imagination.
The page runs slower again, but I now can use the main.tpl for every page.
I could continue on with more to add to this that will make building a website easier and quicker to do but in the end it will always be slower to execute the script. For me, the development time saved orginally, when you want to add features and when bugs appear outweighs the difference in execution time of the script.
Note: caching this page would now be quite easy. Using a cache in php when using OO will make up for any difference in execution speed.
Bookmarks