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

Building a few simple subclasses - 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.

  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



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

// 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>';
foreach($contents as $line){
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){
foreach($contents as $line){
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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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