PHP
  Home arrow PHP arrow Page 5 - 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 
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 - 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





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