PHP
  Home arrow PHP arrow Page 3 - Creating a Blog Application with Interpreter Classes with PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Creating a Blog Application with Interpreter Classes with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 10
    2007-04-16


    Table of Contents:
  • Creating a Blog Application with Interpreter Classes with PHP 5
  • Working with MySQL
  • Defining a basic blog interpreter class
  • The interpreter pattern in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Creating a Blog Application with Interpreter Classes with PHP 5 - Defining a basic blog interpreter class
    ( Page 3 of 4 )

    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.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek