HomePHP 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.
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){ $this->stringSaver=$stringSaver; } // parse string commands public function interpret($command){ if($command!='lowercase'&&$command!='uppercase'&&$command! ='reverse'&&$command!='length'){ throw new Exception('A valid user command must be supplied to parse data string!'); } // parse 'lowercase' command elseif($command=='lowercase'){ return strtolower($this->stringSaver->loadString ()); } // parse 'uppercase' command elseif($command=='uppercase'){ return strtoupper($this->stringSaver->loadString()); } // parse 'reverse' command elseif($command=='reverse'){ return strrev($this->stringSaver->loadString()); } // parse 'length' command else{ 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.