HomePHP Page 4 - Manipulating String Literals with Interpreter Classes in PHP 5
Parsing string commands by using the StringInterpreter 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.
If you’re anything like me, then you want to see how the previous string interpreter class can be used in the context of a hands-on example. Below I coded an easy-to-follow script, which demonstrates how different string commands are parsed by the corresponding interpreter, resulting in the manipulation of a specific input string.
Also, it’s worth clarifying that the following example assumes that a sample string has been saved previously to a default target file. Now, examine the corresponding code listing, which looks like this:
/* displays the following: gnirts tluafed eht si sihT */
// display length of input string echo 'Number of characters of input string is the following : '.$stringInt->interpret('length');
/* displays the following: Number of characters of input string is the following : 26 */ } catch(Exception $e){ echo $e->getMessage(); exit(); }
As shown above, the example first creates an instance of the “StringSaver” class, and uses its “saveString()” method to save a sample string to the default target file. Next, an interpreter object is spawned, and all the string commands accepted by this object are passed to the respective “interpreter()” method.
Of course, the purpose in doing this is simply to uppercase, lowercase and reverse the inputted string, something that is clearly demonstrated by the subsequent calls to the “interpreter()” method.
Finally, the example finishes displaying the number of characters corresponding to the aforementioned input string. Again, in this case the interpreter utilizes the predefined “length” command to perform this basic operation.
So far, so good. At this stage, and after studying the source code that corresponds to the previous practical example, you should have a better idea of how the interpreter pattern does its business. As you saw, the logic behind this pattern is extremely simple, and doesn’t differ too much from creating a conventional parsing class.
Final thoughts
It’s hard to believe, but we’ve come to the end of this second article of the series. In this tutorial, you learned how to implement the interpreter pattern in PHP 5 by using a bunch of simple classes, aimed at processing different input strings in a basic way.
Nonetheless, this is only the prologue for the next (and last) part of the series, since in it I’m going to demonstrate how this handy pattern can be used to develop a decent blog application. The experience sounds really promising and educational. You won’t want to miss it!