Home arrow PHP arrow 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.

  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



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){
     // get formatted number of rows
       throw new Exception('Number of rows not available');
     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.

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