HomePHP Page 2 - Building Interpreter Classes with PHP 5
The basics of implementing the interpreter pattern - 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.
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.