HomePHP 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.
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.