PHP
  Home arrow PHP arrow Page 2 - Manipulating String Literals with Inte...
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
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? 
PHP

Manipulating String Literals with Interpreter Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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


       · Over the course of this second installment of the series, you'll see how to use an...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT