Home arrow PHP arrow Page 4 - Working with Template Classes in PHP 5

Completing the model imposed by the template pattern - PHP

A template class is a base class with some special abilities. First, it concretely implements one or more algorithms; second, it can determine which subclasses should use these algorithms. This article is the first part of a two-part series that introduces you to the basic concepts of this design pattern.

TABLE OF CONTENTS:
  1. Working with Template Classes in PHP 5
  2. Creating a basic implementation of the template pattern
  3. Building a simple template class
  4. Completing the model imposed by the template pattern
  5. Setting up a functional example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 18
March 19, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I expressed in the section that you just read, completing the programmatic model dictated by the template pattern in only a matter of deriving a couple of child classes from the base template class.

Of course, the reason for doing this is simply to understand how a concrete child class can use the specific formatting algorithm defined by the corresponding parent. In this case in particular, Iím going to derive only three subclasses from the base ďResultRemplateĒ class. Each of them will be tasked with applying different (X)HTML formats to a given MySQL data set.

Having explained how the respective child classes are going to work, please have a look at their respective definitions. Here they are:

// define concrete 'ParagraphResultTemplate' class
class ParagraphResultTemplate extends ResultTemplate{
   public function getFormattedNumRows($result){
     return '<h2>Number of rows: '.$result->countRows().'</h2>';
   }
   public function getFormattedRows($result){
     $output='<p>Record listing:</p>';
     while($row=$result->fetchRow()){
       $tempOut='<p>';
       foreach($row as $field){
         $tempOut.=$field;
       }
       $tempOut.='</p>';
       $output.=$tempOut;
     }
     return $output;   
   }
}
// define concrete 'DivResultTemplate' class
class DivResultTemplate extends ResultTemplate{
   public function getFormattedNumRows($result){
     return '<h2>Number of rows: '.$result->countRows().'</h2>';
   }
   public function getFormattedRows($result){
     $output='<div>Record listing:</div>';
     while($row=$result->fetchRow()){
       $tempOut='<div>';
       foreach($row as $field){
         $tempOut.=$field;
       }
       $tempOut.='</div>';
       $output.=$tempOut;
     }
     return $output;   
   }
}
// define concrete 'DivResultTemplate' class
class HeaderResultTemplate extends ResultTemplate{
   public function getFormattedNumRows($result){
     return '<h2>Number of rows: '.$result->countRows().'</h2>';
   }
   public function getFormattedRows($result){
     $output='<h3>Record listing:</h3>';
     while($row=$result->fetchRow()){
       $tempOut='<h3>';
       foreach($row as $field){
         $tempOut.=$field;
       }
       $tempOut.='</h3>';
       $output.=$tempOut;
     }
     return $output;   
   }
}

As you can see, the three subclasses defined above use the formatting algorithm defined by the respective parent to return to client code a string of (X)HTML formatted database rows, and the number of records as well. However, none of these classes overrides the algorithm in question, since itís always utilized as a template. Now, are you starting to grasp the logic that drives the template pattern? Iím sure you are!

So far, so good. At this time, you hopefully understand how this neat pattern does its business. If you're anything like me, though, you want to see how it works in a concrete situation, right?

In the next section Iím going to develop a functional example. It will include all the template classes that were defined previously. Click on the below link and keep reading.



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