PHP
  Home arrow PHP arrow Page 5 - 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? 
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 - Seeing the template pattern in action
    ( Page 5 of 5 )

    As I expressed in the section that you just read, below I included a short script which demonstrates in a friendly fashion the functionality of all the classes that I defined earlier. Also, the script in question assumes that there's a sample "default_file.txt" file that contains the following data:

    This is line 1 of data file
    This is line 2 of data file
    This is line 3 of data file
    This is line 4 of data file
    This is line 5 of data file
    This is line 6 of data file
    This is line 7 of data file
    This is line 8 of data file
    This is line 9 of data file
    This is line 10 of data file

    Having clarified that point, please examine the below example along with the different outputs that it generates:

    try{
     
    // create new 'FileProcessor' object
     
    $fileProc=new FileProcessor();
     
    // create new 'UppercasedFileTemplate' object
     
    $upperFileTemplate=new UppercasedFileTemplate();
     
    // display file contents as uppercased string
      // echo $upperFileTemplate->displayFormattedFileContents
    ($fileProc);

      /*
     
    displays the following:

      SIZE OF DATA FILE IS :280 BYTES
     
    CONTENTS OF DATA FILE ARE AS FOLLOWS :
     
    THIS IS LINE 1 OF DATA FILE
     
    THIS IS LINE 2 OF DATA FILE
     
    THIS IS LINE 3 OF DATA FILE
     
    THIS IS LINE 4 OF DATA FILE
     
    THIS IS LINE 5 OF DATA FILE
     
    THIS IS LINE 6 OF DATA FILE 
      THIS IS LINE 7 OF DATA FILE
     
    THIS IS LINE 8 OF DATA FILE
     
    THIS IS LINE 9 OF DATA FILE
     
    THIS IS LINE 10 OF DATA FILE
     
    */

      // create 'HTMLFileTemplate' object
     
    $htmlFileTemplate=new HTMLFileTemplate();
     
    // display file contents as HTML
     
    //echo $htmlFileTemplate->displayFormattedFileContents
    ($fileProc);

      /* displays the following (formatted as HTML)

      Size of data file is: 280 bytes
     
    Contents of data file are as follows :
     
    This is line 1 of data file
     
    This is line 2 of data file
     
    This is line 3 of data file
     
    This is line 4 of data file
     
    This is line 5 of data file
     
    This is line 6 of data file
     
    This is line 7 of data file
     
    This is line 8 of data file
     
    This is line 9 of data file
     
    This is line 10 of data file
     
    */

      // create 'XMLFileTemplate' object
     
    $xmlFileTemplate=new XMLFileTemplate();
     
    // display file contents as XML
     
    header('Content-Type: text/xml');
     
    echo $xmlFileTemplate->displayFormattedFileContents
    ($fileProc);

      /* displays the following: (formatted as XML)

      <contents>
     
    <size>Size of data file is: 280 bytes</size>
     
    <data>This is line 1 of data file</data>
     
    <data>This is line 2 of data file</data>
     
    <data>This is line 3 of data file</data>
     
    <data>This is line 4 of data file</data>
     
    <data>This is line 5 of data file</data>
     
    <data>This is line 6 of data file</data>
     
    <data>This is line 7 of data file</data>
     
    <data>This is line 8 of data file</data>
     
    <data>This is line 9 of data file</data>
     
    <data>This is line 10 of data file</data>
     
    </contents>
     
    */
    }
    catch(Exception $e){
      echo $e->getMessage();
     
    exit();
    }

    As you can see, the above example demonstrates quite clearly how the template pattern works. All the template subclasses instantiated previously use the algorithm defined by the respective parent to display, in a specific format, data fetched from a specific file.

    One last note before I finish this tutorial: as usual, feel free to tweak the source code of all the classes included here, so you can introduce improvements and acquire a better understanding on how the template pattern works.

    Final thoughts

    That's all for now. In this two-part series, hopefully you expanded your existing background on pattern-based programming with PHP, by learning a new one. Perhaps building template classes won't change your developer life forever, but it'll help you to have a more solid knowledge of how this pattern works.

    See you in the next PHP tutorial!



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - 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
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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