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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 17
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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