HomePHP 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.
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()){ if(count($options)!=self::OPTIONS){ throw new Exception('Invalid number of connection parameters'); } foreach($options as $parameter=>$value){ if(!$value){ throw new Exception('Invalid parameter '.$parameter); } $this->{$parameter}=$value; } $this->connectDB(); } // connect to MySQL private function connectDB(){ if(!$this->conId=mysql_connect($this->host,$this- >user,$this->password)){ throw new Exception('Error connecting to the server'); } if(!mysql_select_db($this->database,$this->conId)){ throw new Exception('Error selecting database'); } } // run query public function query($query){ if(!$this->result=mysql_query($query,$this->conId)){ throw new Exception('Error performing query '.$query); } return new Result($this,$this->result); } } // define 'Result' class class Result{ private $mysql; private $result; public function __construct(&$mysql,$result){ $this->mysql=&$mysql; $this->result=$result; } // fetch row public function fetchRow(){ return mysql_fetch_assoc($this->result); } // count rows public function countRows(){ if(!$rows=mysql_num_rows($this->result)){ throw new Exception('Error counting rows'); } return $rows; } // count affected rows public function countAffectedRows(){ if(!$rows=mysql_affected_rows($this->mysql->conId)){ throw new Exception('Error counting affected rows'); } return $rows; } // get ID form last-inserted row public function getInsertID(){ if(!$id=mysql_insert_id($this->mysql->conId)){ throw new Exception('Error getting ID'); } return $id; } // seek row public function seekRow($row=0){ if(!is_int($row)||$row<0){ throw new Exception('Invalid result set offset'); } if(!mysql_data_seek($this->result,$row)){ 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.