PHP
  Home arrow PHP arrow Page 2 - Working with Template Classes in PHP 5
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Working with Template Classes in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 15
    2007-03-19

    Table of Contents:
  • Working with Template Classes in PHP 5
  • Creating a basic implementation of the template pattern
  • Building a simple template class
  • Completing the model imposed by the template pattern
  • Setting up a functional example

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Working with Template Classes in PHP 5 - Creating a basic implementation of the template pattern


    (Page 2 of 5 )

    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.

    More PHP Articles
    More By Alejandro Gervasio


       · Over the course of this article (the first part of a two-part series), you'll learn...
     

       

    PHP ARTICLES

    - Using Aliases and the Autoload Function with...
    - Authentication Scripts for a User Management...
    - Utilizing the Use Keyword for Namespaces in ...
    - Building a User Management Application
    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Sub Classing Exceptions in PHP 5
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
    Stay green...Green IT