HomePHP 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.
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.