Home arrow PHP arrow Page 3 - Building Interpreter Classes with PHP 5

Parsing predefined commands - PHP

If you have ever written an application that primarily parses commands -- and who hasn't? -- keep reading. As is often the case, pattern-based programming makes this task easier. In this first part of a three-part article series, you'll learn the basic concepts surrounding the Interpreter pattern, with plenty of hands-on examples.

TABLE OF CONTENTS:
  1. Building Interpreter Classes with PHP 5
  2. The basics of implementing the interpreter pattern
  3. Parsing predefined commands
  4. Seeing the interpreter pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
April 02, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: