PHP
  Home arrow PHP arrow Page 2 - 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? 
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 - The basics of implementing the interpreter pattern
    ( Page 2 of 4 )

    According to the concepts deployed in the introduction, in this first article of the series I'm going to demonstrate how to build an interpreter class by developing a simple user managing system. It will perform some useful tasks, such as adding new users to the system in question and displaying basic information associated with them, including their full names and their postal and email addresses.

    Next, I'm going to create an interpreter class, which will be responsible for parsing a set of predefined commands. These commands will come in handy for showing the aforementioned user-related information. However, as you'll see shortly, this process will be sent through the corresponding interpreter, in this way implementing the programmatic model imposed by the homonymous pattern.

    All right, having said that, please take a look at the signature of the following "User" class, which will be useful for handling different users as a bunch of objects. Its definition is listed below:

    // define 'User' class
    class User{
      
    private $name;
      
    private $address;
      
    private $email;
      
    public function __construct($name,$address,$email){
        
    if(!$name){
          
    throw new Exception('A valid name must be supplied!.');
        
    }
        
    if(!$address){
          
    throw new Exception('A valid postal address must be
    supplied!.');
        
    }
        
    if(!$email){
          
    throw new Exception('A valid email address be
    supplied!.');
        
    }
        
    $this->name=$name;
         
    $this->address=$address;
        
    $this->email=$email;
      
    }
      
    // get user name
      
    public function getName(){
        
    return $this->name;
      
    }
      
    // get user postal address
       
    public function getAddress(){
          
    return $this->address;
      
    }
      
    // get user email
      
    public function getEmail(){
        
    return $this->email;
      
    }
    }

    As indicated above, the previous "User" class is merely a computational model that represents a typical user, where his full name and postal/email addresses are stored as class properties. Besides, you should notice that this class presents some useful accessors for retrieving the properties that I mentioned before.

    So far, so good. You shouldn't have any problems understanding the logic followed by the prior "User" class, since as you can see, it's very easy to grasp. However, I stated previously that all these users should be stored somewhere so that they can be handled later on. So, based on this requirement, below I coded a simple class, aimed at saving objects of type "User" as an array structure.

    The signature of this brand new class is as following:

    // define 'UserSaver' class
    class UserSaver{
      
    private $users=array();
      
    // save new user to array
      
    public function save(User $user){
        
    $this->users[]=$user;
      
    }
      
    // load specific user from array
      
    public function load($userIndex){
        
    if(!is_int($userIndex)&&$userIndex<0&&userIndex0>=count
    ($this->users)){
          
    throw new Exception('Invalid user index!.');
        
    }
        
    if(!$user=$this->users[$userIndex]){
          
    throw new Exception('Error retrieving user object!.');
        
    }
        
    return $user;
      
    }
      
    // load all users from array
      
    public function loadAll(){
        
    if(count($this->users)<1){
          
    throw new Exception('No users were saved to array!.');
        
    }
        
    return $this->users;
      
    }
    }

    As you can see, the prior "UserSaver" class performs some useful tasks, like saving different user objects to an internal array, in addition to loading a particular user. Besides, there's an additional method, called "loadAll()," which obviously returns to calling code all the users stored in the aforementioned array.

    Now that you have learned the respective signatures for the previous two classes, it's time to leap forward and jump into the next section, where I'm going to show you how to create a simple interpreter class. In consonance with the schema dictated by this pattern, this class will define (and parse, by the way) a set of specific commands, which will display information about the user objects that you learned previously.

    To see how this interpreter class will be built, please click on the link below and keep reading.



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

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT