PHP
  Home arrow PHP arrow Page 4 - Processing File Data with Template 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

Processing File Data with Template Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2007-03-26


    Table of Contents:
  • Processing File Data with Template Classes in PHP 5
  • Building a basic file processing class
  • Creating a simple template class
  • Building a few simple subclasses
  • Seeing the template pattern in action

  • 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


    Processing File Data with Template Classes in PHP 5 - Building a few simple subclasses
    ( Page 4 of 5 )

    As you'll certainly recall from the previous section, the template class that you saw presented a method called "getFormattedFileContents()," which doesn't have a concrete implementation. Naturally, this fact implies that the method in question must be explicitly defined by all the subclasses derived from the corresponding parent.

    Therefore, I'm going to build three different child classes. These classes will be tasked with displaying the data fetched from a given file in diverse formats. With reference to this, the first class will return file contents as an uppercased string, while the other two will show this data in (X)HTML and XML formats respectively.

    Having explained how these subclasses are going to work, you can now take a look at their respective signatures. They look like this:

    // define concrete 'UppercasedFileTemplate' class
    class UppercasedFileTemplate extends FileTemplate{
      
    public function getFileSize($fileProcessor){
        
    return 'SIZE OF DATA FILE IS :'.strtoupper($fileProcessor-
    >getFileSize()).' BYTES';
      
    }
      
    public function getFormattedFileContents($fileProcessor){
        
    return 'CONTENTS OF DATA FILE ARE AS FOLLOWS :'.strtoupper
    ($fileProcessor->readData());
      
    }
    }

    // define concrete 'HTMLFileTemplate' class
    class HTMLFileTemplate extends FileTemplate{
      
    public function getFileSize($fileProcessor){
        
    return '<html><head><title>Testing Template Pattern</title></head><body><h2>Size of data file is:
    '.$fileProcessor->getFileSize().' bytes</h2>';
      
    }
      
    // display file contents as HTML
      
    public function getFormattedFileContents($fileProcessor){
        
    $output='<p>Contents of data file are as follows :</p>';
        
    $contents=explode("n",$fileProcessor->readData());
        
    foreach($contents as $line){
          
    $output.='<p>'.$line.'</p>';
        
    }
        
    $output.='</body></html>';
        
    return $output;
      
    }
    }

    // define concrete 'XMLFileTemplate' class
    class XMLFileTemplate extends FileTemplate{
      
    public function getFileSize($fileProcessor){
        
    return '<?xml version="1.0" encoding="iso-8859-1"?><contents><size>Size of data file is: '.$fileProcessor-
    >getFileSize().' bytes</size>';
      
    }
      
    // display file contents as XML
      
    public function getFormattedFileContents($fileProcessor){
        
    $output='';
        
    $contents=explode("n",$fileProcessor->readData());
        
    foreach($contents as $line){
          
    $output.='<data>'.$line.'</data>';
        
    }
        
    $output.='</contents>';
        
    return $output;
      
    }
    }

    As you'll possibly agree, the pertinent structures of the above three subclasses are quite simple to understand. As you can see, each one of them accepts an instance of the file processor class that was shown in a previous section, and implements concretely the "getFormattedFileContents()" method that you learned before.

    Of course, the output format applied to the data retrieved from a specified file will depend on the subclass being instantiated, certainly a fact that can be seen clearly when examining each of the subclasses listed above.

    Okay, now that you know how the previous child classes look, the programmatic schema imposed by the template pattern should be familiar to you, since on top of the hierarchy there's a template class that defines a specific formatting algorithm, which is implemented by the respective subclasses.

    Nonetheless, I believe that the functionality of the template pattern is better appreciated by a functional example. In the upcoming section I'm going to build a simple PHP script where all the previous template classes will be put to work in conjunction.

    Want to see how this script will be created? Keep reading, please.



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