PHP
  Home arrow PHP arrow Page 4 - 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 - Completing the model imposed by the template pattern
    ( Page 4 of 5 )

    As I expressed in the section that you just read, completing the programmatic model dictated by the template pattern in only a matter of deriving a couple of child classes from the base template class.

    Of course, the reason for doing this is simply to understand how a concrete child class can use the specific formatting algorithm defined by the corresponding parent. In this case in particular, I’m going to derive only three subclasses from the base “ResultRemplate” class. Each of them will be tasked with applying different (X)HTML formats to a given MySQL data set.

    Having explained how the respective child classes are going to work, please have a look at their respective definitions. Here they are:

    // define concrete 'ParagraphResultTemplate' class
    class ParagraphResultTemplate extends ResultTemplate{
       public function getFormattedNumRows($result){
         return '<h2>Number of rows: '.$result->countRows().'</h2>';
       }
       public function getFormattedRows($result){
         $output='<p>Record listing:</p>';
         while($row=$result->fetchRow()){
           $tempOut='<p>';
           foreach($row as $field){
             $tempOut.=$field;
           }
           $tempOut.='</p>';
           $output.=$tempOut;
         }
         return $output;   
       }
    }
    // define concrete 'DivResultTemplate' class
    class DivResultTemplate extends ResultTemplate{
       public function getFormattedNumRows($result){
         return '<h2>Number of rows: '.$result->countRows().'</h2>';
       }
       public function getFormattedRows($result){
         $output='<div>Record listing:</div>';
         while($row=$result->fetchRow()){
           $tempOut='<div>';
           foreach($row as $field){
             $tempOut.=$field;
           }
           $tempOut.='</div>';
           $output.=$tempOut;
         }
         return $output;   
       }
    }
    // define concrete 'DivResultTemplate' class
    class HeaderResultTemplate extends ResultTemplate{
       public function getFormattedNumRows($result){
         return '<h2>Number of rows: '.$result->countRows().'</h2>';
       }
       public function getFormattedRows($result){
         $output='<h3>Record listing:</h3>';
         while($row=$result->fetchRow()){
           $tempOut='<h3>';
           foreach($row as $field){
             $tempOut.=$field;
           }
           $tempOut.='</h3>';
           $output.=$tempOut;
         }
         return $output;   
       }
    }

    As you can see, the three subclasses defined above use the formatting algorithm defined by the respective parent to return to client code a string of (X)HTML formatted database rows, and the number of records as well. However, none of these classes overrides the algorithm in question, since it’s always utilized as a template. Now, are you starting to grasp the logic that drives the template pattern? I’m sure you are!

    So far, so good. At this time, you hopefully understand how this neat pattern does its business. If you're anything like me, though, you want to see how it works in a concrete situation, right?

    In the next section I’m going to develop a functional example. It will include all the template classes that were defined previously. Click on the below link and keep reading.



     
     
    >>> 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 5 hosted by Hostway
    Stay green...Green IT