Home arrow PHP arrow 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.

  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



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:

   // create 'StringSaver' object
   $stringSaver=new StringSaver();
   // save sample string to destination file
   // create 'StringInterpreter' object
   $stringInt=new StringInterpreter($stringSaver);
   // display lowercased input string
   echo $stringInt->interpret('lowercase');

   /* displays the following:
   this is the default string

   // display uppercased input string
   echo $stringInt->interpret('uppercase');

   displays the following:

   // display reversed input string
   echo $stringInt->interpret('reverse');

   /* displays the following:
   gnirts tluafed eht si sihT

   // display length of input string
   echo 'Number of characters of input string is the following :

   /* displays the following:
   Number of characters of input string is the following : 26
catch(Exception $e){
   echo $e->getMessage();

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!

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