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


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