Home arrow PHP arrow Page 3 - Manipulating String Literals with Interpreter Classes in PHP 5

Defining a string interpreter class - PHP

Among the huge variety of design patterns that can be easily implemented in PHP 5, the interpreter is one that might be particularly interesting to you. It allows you to build effortlessly the so-called parser layers. Welcome to the second installment of the series that began with “Building Interpreter Classes with PHP 5.” Made up of three parts, this series introduces the core concepts on this useful pattern, and also complements its theory with numerous educational hands-on examples.

  1. Manipulating String Literals with Interpreter Classes in PHP 5
  2. Handling literals
  3. Defining a string interpreter class
  4. Parsing string commands by using the StringInterpreter class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
April 09, 2007

print this article



As I expressed in the section that you just read, the only piece required to complete the schema imposed by the interpreter pattern is an interpreter class. This class should be capable of parsing a bunch of predefined commands to process a given data string in some useful way.

Below I listed the definition of a brand new interpreter class. As I explained previously, it will be capable of parsing a number of specific string commands.

Please examine the signature of this string interpreter class, which looks like this:

// define 'StringInterpreter' class
class StringInterpreter{
   private $stringSaver;
   public function __construct(StringSaver $stringSaver){
   // parse string commands
   public function interpret($command){
       throw new Exception('A valid user command must be supplied
to parse data string!');
     // parse 'lowercase' command
       return strtolower($this->stringSaver->loadString
     // parse 'uppercase' command
       return strtoupper($this->stringSaver->loadString());
     // parse 'reverse' command
       return strrev($this->stringSaver->loadString());
     // parse 'length' command
       return strlen($this->stringSaver->loadString());

As you can see, the logic implemented by the prior “StringInterpreter” class is fairly easy to follow. Similar to the interpreter that I created in the preceding article of the series, this class also presents a method, called “interpret(),” which not surprisingly is tasked with parsing a predefined number of string commands.

In this case, the class in question has been provided with the ability to execute four different instructions, named “lowercase,” “uppercase,” “reverse” and finally “length.” Obviously, since the names of these commands are extremely intuitive, I’m sure that you’ve already guessed that they’re used to uppercase, lowercase and reverse a given input string, in addition to calculating its length.

Nevertheless, it’s worthwhile to note here that all these simple string-related operations are performed internally by the interpreter by using the “loadString()” method that belongs to the “StringSaver” class. Simple to understand, right?

Okay, at this point I believe that you already understand how the previously-defined string interpreter class works. At this point I imagine you want to see for yourself how this class can be used in a concrete situation. Therefore, in the upcoming section I’m going to develop a simple example to demonstrate how the class in question can be capable of parsing the wealth of string commands that were discussed earlier.

To see how this practical example will be created, click on the link below and keep reading.

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