PHP
  Home arrow PHP arrow Page 3 - 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 - Parsing predefined commands
    ( Page 3 of 4 )

    As you might have guessed, building an interpreter class with PHP 5 is indeed a straightforward process that can be performed with minor hassles, since its main task is parsing a group of specific commands that eventually will do something useful.

    In this particular case, the interpreter class that I'm about to define will be tasked with displaying information about the user objects that you saw in the previous section, by using a bunch of predefined instructions.

    Here is the corresponding signature of the interpreter class in question. Take a look at it, please:

    // define 'UserInterpreter' class
    class UserInterpreter{
      
    private $userSaver;
      
    public function __construct(UserSaver $userSaver){
        
    $this->userSaver=$userSaver;
      
    }
      
    // parse user command
      
    public function interpret($command,$userIndex){
        
    if($command!='name'&&$command!='address'&&$command!
    ='email'&&$command!='all'){
          
    throw new Exception('A valid user command must be
    supplied to parse user data.');
        
    }
        
    // parse 'name' command
        
    if($command=='name'){
          
    $user=$this->userSaver->load($userIndex);
          
    return $user->getName();
        
    }
        
    // parse 'address' command
        
    elseif($command=='address'){
          
    $user=$this->userSaver->load($userIndex);
          
    return $user->getAddress();
        
    }
        
    // parse 'email' command
        
    elseif($command=='email'){
          
    $user=$this->userSaver->load($userIndex);
          
    return $user->getEmail();
        
    }
        
    // parse 'all' command
        
    else{
          
    $users=$this->userSaver->loadAll();
          
    $output='';
          
    foreach($users as $user){
            
    $output.='Name : '.$user->getName().' Postal Address :
    '.$user->getAddress().' Email : '.$user->getEmail().'<br />';
          
    }
          
    return $output;
         
    }
      
    }
    }

    After examining the definition of the previous interpreter class, you'll have to agree with me that it's pretty easy to grasp. After all, all this class does is parse four specific commands, called "name," "address," "email" and "all" respectively, in order to display information associated with a specific user (hence the name "UserInterpreter").

    Nonetheless, in this case it's worth noticing that the interpreter performs all the tasks by using the methods of a "UserSaver" object, which is inputted directly into the corresponding class via its constructor.

    Finally, the interpreter has the capacity to parse a command called "all." This command displays data about all the users stored by the respective "UserSaver" object. Logically, this process is very easy to follow, so I won't spend a long time explaining how it works.

    Okay, at this stage you hopefully learned how an interpreter class does its thing, therefore it's a good time to move forward and see how this class can be used in the context of a practical example. In doing so, you'll understand more clearly how user-related information can be displayed by using the functionality provided by the interpreter pattern.

    To see how this brand new hands-on example will be developed, jump ahead and visit the next 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek