HomePHP Page 3 - Creating a Blog Application with Interpreter Classes with PHP 5
Defining a basic blog interpreter class - PHP
Working with interpreter classes in PHP 5 can be a useful experience for any PHP developer. Welcome to the final part of the series that began with “Building Interpreter Classes with PHP 5.” Composed of three educational tutorials, this series teaches you how to implement the interpreter pattern with PHP, by covering not only the corresponding theoretical concepts, but also showing you concrete cases where this pattern can be applied in a useful way.
In accordance with the concepts that I just deployed in the prior section, I'm going to build a simple PHP class. It will be merely a logical representation of a typical blog entry, where all its attributes, including its title, author and content will be saved as class properties.
After defining this blog class, I'm going to create a couple of additional classes for processing the corresponding blog entries by using the interpreter pattern. But I'm getting ahead of myself, so for the moment, please examine the signature of the class below. It represents a conventional blog entry. Its definition is as follows:
// define 'Blog' class class Blog{ private $title; private $author; private $text; public function __construct($title,$author,$text){ if(!$title){ throw new Exception('A valid blog title must be supplied!.'); } if(!$author){ throw new Exception('A valid blog author must be supplied!.'); } if(!$text||strlen($text)>1024){ throw new Exception('A valid blog text must be supplied!.'); } $this->title=$title; $this->author=$author; $this->text=$text; } // get blog title public function getTitle(){ return $this->title; } // get blog author public function getAuthor(){ return $this->author; } // get blog text public function getText(){ return $this->text; } }
As indicated above, the "Blog" class that I just defined is a basic representation of a simple blog entry, where all its properties can be easily retrieved via the corresponding accessing methods. As you can see, the logic implemented by this class is in fact very easy to grasp, so you shouldn't have major problems understanding how it works.
Well, now that you have learned the definition corresponding to the previous "Blog" class, have a look at the respective signatures of the following classes, which are tasked with inserting, updating and deleting blog entries by implementing the interpreter pattern.
Having said that, these brand new classes look like this:
// define 'BlogHandler' class class BlogHandler{ private $mysql; public function __construct(MySQL $mysql){ $this->mysql=$mysql; } // add new blog public function insertBlog(Blog $blog){ $title=$blog->getTitle(); $author=$blog->getAuthor(); $text=$blog->getText(); $this->mysql->query("INSERT INTO blogs (id,author,title,text) VALUES (NULL,'$author','$title','$text')"); } // update existing blog public function updateBlog(Blog $blog,$id){ $title=$blog->getTitle(); $author=$blog->getAuthor(); $text=$blog->getText(); $this->mysql->query("UPDATE blogs SET title='$title',author='$author',text='$text' WHERE id='$id'"); } // delete blog public function deleteBlog($id){ $this->mysql->query("DELETE FROM blogs WHERE id='$id'"); } }
// define 'BlogInterpreter' class class BlogInterpreter{ private $blogHandler; public function __construct(BlogHandler $blogHandler){ $this->blogHandler=$blogHandler; } public function interpret($blogCommand,Blog $blog,$id=''){ if($blogCommand!='insert'&&$blogCommand! ='update'&&$blogCommand!='delete'){ throw new Exception('A valid blog command must be supplied!.'); } // parse 'insert' command if($blogCommand=='insert'){ $this->blogHandler->insertBlog($blog); } // parse 'update' command elseif($blogCommand=='update'){ $this->blogHandler->updateBlog($blog,$id); } // parse 'delete' command else{ $this->blogHandler->deleteBlog($id); } } }
If you studied the signatures of the above two classes, then you'll have to agree with me that they demonstrate in a friendly fashion how to build a basic blog application that uses the schema imposed by the interpreter pattern.
As you can see, the first "BlogHandler" class is responsible for interfacing directly with MySQL to perform the insertion, update and deletion of blog entries respectively, while the second class, called "BlogInterpreter," is tasked with parsing a set of predefined commands for performing all the aforementioned blog-related operations.
Now do you see how the interpreter pattern is used in this case to build this simple blog application? I'm sure you do!
All right, at this point you hopefully understand how all the previous classes do their business, therefore I think it's time to show how they can be used all together in one practical example. This will demonstrate the functionality of the interpreter pattern.
Want to see how this concrete example will be developed? Go ahead and read the following section. I'll be there, waiting for you.