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

Creating a basic implementation of 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.

  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



Demonstrating the functionality of a template class is a process that can be achieved with only minor problems. First Iím going to define primarily two MySQL wrapping classes, which will be used for fetching database result sets. Then Iíll build a simple template class which will be responsible for setting up an algorithm for formatting these data sets.

Having explained how I plan to implement the template pattern with PHP, here are the pertinent signatures that correspond to the MySQL processing classes that I mentioned before: 

// define 'MySQL' class
class MySQL{
   private $conId;
   private $host;
   private $user;
   private $password;
   private $database;
   private $result;
   const OPTIONS=4;
   public function __construct($options=array()){
       throw new Exception('Invalid number of connection
     foreach($options as $parameter=>$value){
         throw new Exception('Invalid parameter
   // connect to MySQL
   private function connectDB(){
       throw new Exception('Error connecting to the
       throw new Exception('Error selecting database');
   // run query
   public function query($query){
       throw new Exception('Error performing query
     return new Result($this,$this->result);
// define 'Result' class
class Result{
   private $mysql;
   private $result;
   public function __construct(&$mysql,$result){
   // fetch row
   public function fetchRow(){
     return mysql_fetch_assoc($this->result);
   // count rows
   public function countRows(){
       throw new Exception('Error counting rows');
     return $rows;
   // count affected rows
   public function countAffectedRows(){
       throw new Exception('Error counting affected rows');
     return $rows;
   // get ID form last-inserted row
   public function getInsertID(){
       throw new Exception('Error getting ID');
     return $id;
   // seek row
   public function seekRow($row=0){
       throw new Exception('Invalid result set offset');
       throw new Exception('Error seeking data');

As you can see, the two MySQL wrapping classes shown above are pretty familiar, since I used them in some of my previous PHP articles. However, the classes in question arenít the primary objective of this tutorial, since I only plan to use them uniquely to fetch data from a MySQL database. Thatís all.

Besides, as I expressed before, Iím going to create a template class to define a specific algorithm. The algorithm will be aimed at formatting the returned result sets. Sounds pretty simple, right?

Okay, now that the respective signatures of the previous MySQL wrapping classes are fresh in your mind, letís learn how to build the template class previously mentioned. Keep reading, please.

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