HomePHP Page 3 - Working with Template Classes in PHP 5
Building a simple template class - 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.
According to the definition of the template pattern, there’s a base class that establishes a specific algorithm, which will also be used for the pertinent subclasses. As you can see, the definition sounds quite simple, but it may be a bit harder to grasp if you try to turn it into functional PHP code.
Therefore, bearing in mind this possible inconvenience, I'm going to create a sample template class. As you’ll see in a few moments, this class will set up a concrete algorithm for formatting MySQL result sets.
Having explained that, the signature that corresponds to this new template class is as follows:
// define abstract 'ResultTemplate' class abstract class ResultTemplate{ // define template method (this is the algorithm setup by the base class) public final function displayFormattedResult($result){ $output=''; // get formatted number of rows if($this->getFormattedNumRows($result)==NULL){ throw new Exception('Number of rows not available'); } $output=$this->getFormattedNumRows($result).$this- >getFormattedRows($result); return $output; } // this sets the generic method to format number of database rows public function getFormattedNumRows($result){ return NULL; } // this sets the generic method to format database rows public function getFormattedRows($result){} }
After examining the definition of the above “ResultTemplate” class, definitely you’ll have to agree with me that things are getting really interesting. As you can see, this template class has been defined as abstract, but it also presents an important method called “displayFormattedResult().”
Logically, the referenced method is responsible for setting up a specific algorithm for returning to calling code not only a string of formatted data sets, but the number of rows returned by the corresponding SELECT statement.
Besides, it’s worthwhile to mention here the existence of two other methods, named “getFormattedNumRows()” and “getFormattedRows()” respectively. These methods are used in conjunction to display the aforementioned formatted data sets.
Nonetheless, the signature of the previous “ResultTemplate” class demonstrates in a nutshell how the template pattern works, since this class defines an algorithm concretely (not abstractly), which will eventually be used by the respective child classes. Pretty simple to grasp, isn’t it?
Okay, at this stage you already learned how the prior template class was built, but there are a couple of missing elements in this schema. Since the template in question is seated on top of the hierarchy of classes, it’s necessary to create some subclasses so that you can see how they interact with each other.
As you may have guessed, these subclasses will be created in the section to come, so keep reading to learn more.