PHP
  Home arrow PHP arrow Page 4 - Processing File Data with Template Cla...
Dev Shed Forums 
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 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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

Processing File Data with Template Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 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:
      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


    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


       · Over the course of this final installment of the series, what you’re going to learn...
     

       

    PHP ARTICLES

    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway