PHP
  Home arrow PHP arrow Page 4 - Building Interpreter Classes with PHP ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

Building Interpreter Classes with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 8
    2007-04-02

    Table of Contents:
  • Building Interpreter Classes with PHP 5
  • The basics of implementing the interpreter pattern
  • Parsing predefined commands
  • Seeing the interpreter pattern in action

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


    Building Interpreter Classes with PHP 5 - Seeing the interpreter pattern in action


    (Page 4 of 4 )

    As you'll certainly recall from the previous section, I said that the best way to understand how the interpreter pattern works is by developing a concrete example, where all the classes that were defined previously are put to work together.

    Basically, what is expected here is that the interpreter class must be capable of parsing the appropriate commands passed in to its "interpret()" method, in this way displaying information about one or more user objects.

    So, considering the expectations that you may have concerning the implementation of the previous interpreter class, below I included a short script, which demonstrates the functionality of this pattern.

    The corresponding code sample is as follows:

    try{
      
    // create some fictional users
      
    $user1=new User('John Doe','Binary Avenue
    1234','john@domain.com');
      
    $user2=new User('Mary Jackson','Port 80
    Boulevard','mary@domain.com');
      
    $user3=new User('Alfred Smith','Scripted Street
    101010','alfred@domain.com');
      
    // create 'UserSaver' object
      
    $userSaver=new UserSaver();
      
    // save fictional users via 'UserSaver' object
      
    $userSaver->save($user1);
      
    $userSaver->save($user2);
      
    $userSaver->save($user3);
      
    // create 'UserInterpreter' object
      
    $userInt=new UserInterpreter($userSaver);
      
    // display name of first user
      
    echo $userInt->interpret('name',0);

      
    /*
      
    displays the following:
      
    John Doe
      
    */

       // displays postal address of first user
      
    echo $userInt->interpret('address',0);

       /*
      
    displays the following:
      
    Binary Avenue 1234
      
    */

       // display email of first user
      
    echo $userInt->interpret('email',0);

       /*
      
    displays the following:
      
    john@domain.com
      
    */

       // display name of second user
      
    echo $userInt->interpret('name',1);

       /*
      
    displays the following:
      
    Mary Jackson
      
    */

       // display postal address of second user
      
    echo $userInt->interpret('address',1);

       /*
      
    displays the following:
      
    Port 80 Boulevard
      
    */

       // display email of second user
      
    echo $userInt->interpret('email',1);

       /*
      
    displays the following:
      
    mary@domain.com
      
    */

       // display name of last user
      
    echo $userInt->interpret('name',2);

       /*
      
    displays the following:
      
    Alfred Smith
       */


      
    // displays postal address of last user
      
    echo $userInt->interpret('address',2);

       /*
      
    displays the following:
      
    Scripted Street 101010
      
    */

       // display email of last user
      
    echo $userInt->interpret('email',2);

       /*
      
    displays the following:
      
    alfred@domain.com
      
    */

       // send a erroneous command to the interpreter
      
    //echo $userInt->interpret('age',2);

       /* throws an exception with the following message:
      
    A valid user command must be supplied to parse user data.
      
    */

       // display data about all users at once
      
    echo $userInt->interpret('all',0);

       /*
      
    displays the following:
      
    Name : John Doe Postal Address : Binary Avenue 1234 Email :
    john@domain.com
      
    Name : Mary Jackson Postal Address : Port 80 Boulevard
    Email : mary@domain.com
      
    Name : Alfred Smith Postal Address : Scripted Street 101010
    Email : alfred@domain.com
       */
    }
    catch(Exception $e){
     
    echo $e->getMessage();
     
    exit();
    }

    If you study the above example, then you'll realize how powerful the interpreter class can be when it comes to displaying information about one or more users. As you can see, the script begins by creating some fictional users, which are saved onto an instance of the "UserSaver" class, and then uses the interpreter object to show data about each one of the stored users.

    Of course, this entire process is performed by sending out to the interpreter the correct user commands. This means that this simple implementation of the pattern in question can be really helpful for building a basic abstraction layer for handling user data. Quite good, isn't it?

    As homework, try adding more users to the previous example, and see what happens when you send erroneous commands to the interpreter. Trust me, the experience can be truly fun!

    Final thoughts

    In this first tutorial of the series, I walked you through the basics of how to implement the interpreter pattern in PHP 5. However, this is only the beginning, because in the next part I'm going to show you how to use this handy pattern in conjunction with some string processing classes. Now that you're warned, are you going to miss it? I hope not!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · Over the course of this first part of the article series, you'll learn the basic...
     

       

    PHP ARTICLES

    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT