PHP
  Home arrow PHP arrow Page 4 - 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 - Parsing string commands by using the StringInterpreter class
    ( Page 4 of 4 )

    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:

    try{
       // create 'StringSaver' object
       $stringSaver=new StringSaver();
       // save sample string to destination file
       $stringSaver->saveString();
       // 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:
       THIS IS THE DEFAULT STRING
       */

       // 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 :
    '.$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!



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