PHP
  Home arrow PHP arrow Page 3 - Manipulating String Literals with Interpreter Classes in PHP 5
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PHP

Manipulating String Literals with Interpreter Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 5
    2007-04-09


    Table of Contents:
  • Manipulating String Literals with Interpreter Classes in PHP 5
  • Handling literals
  • Defining a string interpreter class
  • Parsing string commands by using the StringInterpreter class

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Manipulating String Literals with Interpreter Classes in PHP 5 - Defining a string interpreter class
    ( Page 3 of 4 )

    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.



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek