PHP
  Home arrow PHP arrow Page 4 - Building 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

Building Interpreter Classes with PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      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


    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!



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek