PHP
  Home arrow PHP arrow Page 3 - 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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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 - 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


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

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





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