Simple Blog – The Index

Simple Blog is a multi-part series about developing data-driven application with PHP and MySql. Check out the intro for details about this unique approach.

Advertisements

Simple Blog – Performance Benchmark

Simple Blog is a multi-part series. Check out The Index

In previous posts we designed a simple blog application using a variety of programming styles, patterns, and external libraries. Using’s Apache’s benchmark tool I’ve ran some tests to benchmark each different approach. Each benchmark hammers the respective application with 1000 pages requests across 10 concurrent sessions. The page we’ll call 1000 times is the List page which retrieves and displays all blog posts.

Here’s a list of the commands we’ll be running:

ab -c 10 -n 1000 http://baseurl/1-structured/index.php
ab -c 10 -n 1000 http://baseurl/2-procedural/index.php
ab -c 10 -n 1000 http://baseurl/3-objects/index.php
ab -c 10 -n 1000 http://baseurl/4-mvc/index.php
ab -c 10 -n 1000 http://baseurl/5-mysqli/index.php
ab -c 10 -n 1000 http://baseurl/6-pdo/index.php
ab -c 10 -n 1000 http://baseurl/7-smarty/index.php
ab -c 10 -n 1000 http://baseurl/8-codeigniter/index.php

Here’s what the output looks like for the first test:

Continue reading

Simple Blog – Example 8: CodeIgniter

Simple Blog is a multi-part series. Check out The Index

In example 4, we implemented the MVC pattern for our Simple Blog app. We physically separated our data, presentation, and application logic into models, views, and controllers. In example 6, we rewrote our data logic using PDO which enabled parameterized statements and database portability. In example 7, we implemented the Smarty Template Engine to cleanup our views with a more readable syntax.

So, we’ve developed a web application written in PHP5 that follows MVC, supports multiple databases, provides object-record mapping, has a built-in template engine, and built-in caching. Many of the features we’ve implemented for Simple Blog are precisely the features offered by PHP Frameworks. So, the next logical step was to rewrite the app using a PHP Framework such as CakePHP, CodeIgniter, Symfony, Yii, or Zend which promise to deliver all of the features we’ve implemented so far and more.

In the process of researching these frameworks, I ran across CodeIgniter’s User Guide which was so well written and so well organized that I decided to start with CodeIgniter. A quick glance at codeigniter.com and we get to the promises: small footprint, exceptional performance, nearly zero configuration, and clear documentation. Well, let’s dive in and see what CodeIgniter is all about.

Continue reading

Simple Blog – Example 7: Smarty

Simple Blog is a multi-part series. Check out The Index

In the previous example, we improved our application’s models by rewriting the Database and Post classes using PHP Data Objects. In this example, we’ll shift focus to our application’s views. Until now, we’ve been using PHP’s own engine to render our view pages. Our view pages were made up of HTML interspersed with small block of PHP to import header and footer markup, iterate through arrays, and display the contents of variables. In this example, will integrate the Smarty Template Engine into our application which promises to truly separate presentation logic (HTML/CSS) from application logic (PHP).

In the process of researching this post, I hit up smarty.net to read up on the advantages of implementing their template engine. They claim that Smarty makes the development process easier, faster, and more maintainable. On the flip side, Smarty openly acknowledges that installing their template engine results in slower script execution as compared to native PHP. Obviously, introducing an additional layer of complexity to page rendering will result in slower execution; but how much slower? We’ll get to that …

So, let’s take a look at what Smarty has to offer. We’ll start by looking at our configuration file, where we’ve made a few administrative changes to get Smarty working. FYI, if you’re following along, take a look at Smarty’s quick install guide before proceeding.

Configuration File

Instead of listing the entire config file, I’d like to highlight just the lines relevant to installing Smarty. First, we define an absolute path to the Smarty directory:

define('SMARTY_PATH', INCLUDE_PATH.'smarty'.DS);

Next, we import the Smarty class:

require_once(SMARTY_PATH.'libs'.DS.'Smarty.class.php');

Continue reading

Simple Blog – Example 5: MySQLi

Simple Blog is a multi-part series. Check out The Index

Until now we’ve been exploring different programming styles. We programmed the Simple Blog application using structured, procedural, and object oriented programming. Next, we explored the MVC pattern by physically separating our data, application, and presentation logic. So far, each example has required pervasive changes to the application. For the next few examples, however, we’ll focus on improving specific layers of the application, rather than rewriting the application as a whole.

In this example we’ll be revisiting Simple Blog’s data access layer. PHP offers three options for interacting with a MySQL database: mysql, mysqli, and pdo. So far, we’ve only used the original mysql interface. So, let’s rewrite the Database and Post classes using mysqli. The two mysqli benefits I’m most interested in are the object-oriented interface and the support for prepared statements.

Database Class

Since mysqli already offers an object oriented interface, I decided not to write a wrapper class for mysqli like we did for the original mysql interface. The new Database class extends the mysqli class and performs the simple task of connecting to the database engine and selecting the target database.

class Database extends mysqli 
{
	private $hostname;
	private $username;
	private $password;
	private $database;
	
	public function __construct() 
	{
		// Initialize object with database constants
		$this->hostname = DATABASE_HOST;
		$this->username = DATABASE_USER;
		$this->password = DATABASE_PASSWORD;
		$this->database = DATABASE_NAME;	
		
	    // Open database connection
		parent::__construct(
			$this->hostname, 
			$this->username, 
			$this->password, 
			$this->database
			);
	}

}

Continue reading

Simple Blog – Example 4: MVC

Simple Blog is a multi-part series. Check out The Index

In example 1, we started out by writing some PHP scripts that executed pretty much top to bottom. In example 2, we re-factored our code and markup into reusable function and templates to minimize repetition in the development process. In example 3, we identified a couple candidate objects which we rolled into classes to enhance code maintainability. Throughout this process, we’ve extracted our data logic from our base pages, however, application logic and presentation layer are still mixed.

In this example, we’ll decouple application logic and presentation logic by moving all of our presentation logic into new files we’ll call views. Once we’ve performed this decoupling, our code will be nicely separated into three distinct types: models, views, and controllers.

Models

Our model is just the Post class we created in Example 3. I’ve renamed the file to ‘post.model.php’ and moved it into a directory called models, but other than these two administrative changes, the Post class is unchanged.

Views

Simple Blog has three views: List View, Read View, and Upsert View. The List View displays a list of posts. The Read View displays a single post. The Upsert View displays an HTML form for either creating a new post or editing an existing post. So, let’s take a look at our three views. Note, there is zero new code here, the only thing we’re changing is how the code is organized.

List View

<?php require_once(VIEW_PATH.'header.inc.php'); ?>

	<?php foreach($posts as $post): ?>

		<h4>
			<a href="read.php?id=<?php 
				echo $post->id;?>"><?php 
				echo $post->title;?>
			</a>
		</h4>

		<p>
			<?php echo $post->content;?>
			<?php echo $post->created;?>
		</p>

	<?php endforeach; ?>

<?php require_once(VIEW_PATH.'footer.inc.php'); ?>

Continue reading

Simple Blog – Example 3: Object Oriented

Simple Blog is a multi-part series. Check out The Index

In the previous example, Simple Blog – Example 2, we rolled-up our repetitive code and markup into functions and templates. Furthermore, we grouped functions according to their problem domain. For example, we pushed all of the generic database operations into a file called database.inc.php and pushed all of the post operations into a file called post.inc.php.

In this exercise, we’ll take each of these function groups and roll them into their own classes. First, we’ll create a Database class to facilitate basic database operations such as connect, execute, fetch, and close. Next, we’ll create a Post class to automate the process of selecting, updating, inserting, and deleting data from our post database table.

Database Class

First, let’s take a quick look at the overall layout of the database class.


	class Database 
	{
		private $connection;
		private $hostname;
		private $username;
		private $password;
		private $database;
		
		public function __construct()
		{
			...
		}

		public function openConnection()
		{
			...
		}

		public function closeConnection()
		{
			...
		}

		public function executeStatement($statement)
		{
			...
		}
	
		public function executeSql($sql)
		{
			...
		}

		public function executeDml($dml)
		{
			...
		}
		
		public function sanitizeInput($value)
		{
			...
		}
	}

Now, let’s look at a few key methods in the Database class.

Continue reading