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.

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

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.



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 11 - Follow our Sitemap

Dev Shed Tutorial Topics: