Home arrow PHP arrow Page 5 - Processing File Data with Template Classes in PHP 5

Seeing the template pattern in action - PHP

If you're looking for a friendly guide on how to implement the template design pattern with PHP 5, then this group of articles might be what you need. Welcome to the final installment of the series that began with "Working with template classes in PHP 5." As the title indicates, this series steps you through creating template classes in PHP-controlled development environments, and also shows you how to use this rather uncommon pattern in real-world situations.

TABLE OF CONTENTS:
  1. Processing File Data with Template Classes in PHP 5
  2. Building a basic file processing class
  3. Creating a simple template class
  4. Building a few simple subclasses
  5. Seeing the template pattern in action
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 26, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: