PHP
  Home arrow PHP arrow Page 2 - 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 - Handling literals
    ( Page 2 of 4 )

    In accordance with the concepts expressed in the introduction, I’m going to demonstrate how to implement the interpreter pattern by developing a simple mechanism which will come in handy for processing basic string literals.

    Essentially, I’m going to define a class which will perform a few useful tasks. These tasks will include loading and saving an inputted string to a specified text file, as well as calculating the length of the string in question. As you can see, all these tasks sound fairly easy to execute, meaning that the structure of this string processing class is very simple.

    And speaking of that, here is the definition for this class, so take a look at its source code:

    // define 'StringSaver' class
    class StringSaver{
       private $dataFile;
       private $data;
       public function __construct($data='This is the default
    string',$dataFile='datafile.txt'){
         if(empty($data)){
           throw new Exception('Input data must not be an empty
    string!');
         }
         if(!file_exists($dataFile)){
           throw new Exception('Invalid destination file!');
         }
         $this->data=$data;
         $this->dataFile=$dataFile;
       } 
       // load string from destination file
       public function loadString(){
         if(!$this->data=file_get_contents($this->dataFile)){
           throw new Exception('Error reading string from destination
    file!');
         }
         return $this->data;
       }
       // save string to destination file
       public function saveString(){
         if(!$fp=fopen($this->dataFile,'w')){
           throw new Exception('Error opening destination file!');
         }
         if(!fwrite($fp,$this->data)){
           throw new Exception('Error writing string to destination
    file!');
         }
         fclose($fp);
       }
       // determine length of string
       public function getStringLength(){
         if(!$length=strlen($this->data)){
           throw new Exception('Error determining length of data
    string!');
         }
         return $length;
       }
    }

    Not surprisingly, the signature that corresponds to the previous “StringSaver” class is really easy to grasp. As I said before, the tasks performed by this class are limited to loading and saving a given string to a target text file, along with returning the length of that string to calling code.

    However, at this point there’s not a single clue about how to include an interpreter class that can work in conjunction with the string processor that you learned a few lines above. In the next section, I’m going to define the basic structure of an interpreter class, in this way implementing the programmatic model dictated by the homonymous pattern.

    Having said that, and assuming that you’re interested in learning how this interpreter class will be built, I suggest you click on the link that appears below and read the following section.



     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek