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

Seeing the interpreter pattern in action - 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.

  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



As you'll certainly recall from the previous section, I said that the best way to understand how the interpreter pattern works is by developing a concrete example, where all the classes that were defined previously are put to work together.

Basically, what is expected here is that the interpreter class must be capable of parsing the appropriate commands passed in to its "interpret()" method, in this way displaying information about one or more user objects.

So, considering the expectations that you may have concerning the implementation of the previous interpreter class, below I included a short script, which demonstrates the functionality of this pattern.

The corresponding code sample is as follows:

// create some fictional users
$user1=new User('John Doe','Binary Avenue
$user2=new User('Mary Jackson','Port 80
$user3=new User('Alfred Smith','Scripted Street
// create 'UserSaver' object
$userSaver=new UserSaver();
// save fictional users via 'UserSaver' object
// create 'UserInterpreter' object
$userInt=new UserInterpreter($userSaver);
// display name of first user
echo $userInt->interpret('name',0);

displays the following:
John Doe

   // displays postal address of first user
echo $userInt->interpret('address',0);

displays the following:
Binary Avenue 1234

   // display email of first user
echo $userInt->interpret('email',0);

displays the following:

   // display name of second user
echo $userInt->interpret('name',1);

displays the following:
Mary Jackson

   // display postal address of second user
echo $userInt->interpret('address',1);

displays the following:
Port 80 Boulevard

   // display email of second user
echo $userInt->interpret('email',1);

displays the following:

   // display name of last user
echo $userInt->interpret('name',2);

displays the following:
Alfred Smith

// displays postal address of last user
echo $userInt->interpret('address',2);

displays the following:
Scripted Street 101010

   // display email of last user
echo $userInt->interpret('email',2);

displays the following:

   // send a erroneous command to the interpreter
//echo $userInt->interpret('age',2);

   /* throws an exception with the following message:
A valid user command must be supplied to parse user data.

   // display data about all users at once
echo $userInt->interpret('all',0);

displays the following:
Name : John Doe Postal Address : Binary Avenue 1234 Email :
Name : Mary Jackson Postal Address : Port 80 Boulevard
Email : mary@domain.com
Name : Alfred Smith Postal Address : Scripted Street 101010
Email : alfred@domain.com
catch(Exception $e){
echo $e->getMessage();

If you study the above example, then you'll realize how powerful the interpreter class can be when it comes to displaying information about one or more users. As you can see, the script begins by creating some fictional users, which are saved onto an instance of the "UserSaver" class, and then uses the interpreter object to show data about each one of the stored users.

Of course, this entire process is performed by sending out to the interpreter the correct user commands. This means that this simple implementation of the pattern in question can be really helpful for building a basic abstraction layer for handling user data. Quite good, isn't it?

As homework, try adding more users to the previous example, and see what happens when you send erroneous commands to the interpreter. Trust me, the experience can be truly fun!

Final thoughts

In this first tutorial of the series, I walked you through the basics of how to implement the interpreter pattern in PHP 5. However, this is only the beginning, because in the next part I'm going to show you how to use this handy pattern in conjunction with some string processing classes. Now that you're warned, are you going to miss it? I hope not!

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: