Home arrow PHP arrow Page 4 - Creating a Blog Application with Interpreter Classes with PHP 5

The interpreter pattern in action - 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.

TABLE OF CONTENTS:
  1. Creating a Blog Application with Interpreter Classes with PHP 5
  2. Working with MySQL
  3. Defining a basic blog interpreter class
  4. The interpreter pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 10
April 16, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the section that you just went through, below I included an illustrative example of how to use the interpreter class to add some blog entries to a sample "blogs" database, update one of them, and finally delete another entry.

Of course, all of these MySQL-related operations are performed according to the commands accepted by the interpreter. This clearly demonstrates the functionality provided by this pattern.

That being said, here is how the example in question looks:

try{
   // connect to MySQL
   $db=new MySQL(array('host'=>'host','user'=>'user','password'=>'password',
'database'=>'database'));
   // create 'BlogHandler' object
   $blogHandler=new BlogHandler($db);
   // create 'BlogInterpreter' object
   $blogInt=new BlogInterpreter($blogHandler);
   // create some sample blog entries
   $blog1=new Blog('Blog 1','Alejandro Gervasio','This is the
content of blog 1');
   $blog2=new Blog('Blog 2','John Doe','This is the content of
blog 2');
   $blog3=new Blog('Blog 3','Susan Smith','This is the content of
blog 3');
   // insert sample blogs into database
   /*
   $blogInt->interpret('insert',$blog1);
   $blogInt->interpret('insert',$blog2);
   $blogInt->interpret('insert',$blog3);
   */
   // update existing blog
   /*
   $blog2=new Blog('Updated Blog 2','John Doe','This is the
updated content of blog 2');
   $blogInt->interpret('update',$blog2,2);
   */
   // delete existing blog
   //$blogInt->interpret('delete',$blog3,3);
}
catch(Exception $e){
   echo $e->getMessage();
   exit();
}

As shown in the above example, implementing a basic blog application using the schema dictated by the interpreter pattern is actually a straightforward process, which can be achieved with minor efforts. In this case, the previous example first creates three different blog objects, which are inserted into the corresponding database table via the interpreter.

Next, this time following a similar approach, the second blog entry is updated, and finally another row is deleted from the pertinent database table. Of course, as you see, all of these operations are performed by using the interface provided by the interpreter object. Quite simple to achieve, right?

Finally, I'd like to point out one last thing before I finish this tutorial: feel free to modify the source code of all the classes shown here, so you can eventually have a more solid background on how the interpreter pattern works.

Final thoughts

Sadly, we've come to the end of this series. I hope that all the code samples included here will be useful enough to expand your existing knowledge of pattern-based programming with PHP.

See you in the next PHP tutorial!



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: