PHP
  Home arrow PHP arrow Page 3 - 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 - Building a simple template class
    ( Page 3 of 5 )

    According to the definition of the template pattern, there’s a base class that establishes a specific algorithm, which will also be used for the pertinent subclasses. As you can see, the definition sounds quite simple, but it may be a bit harder to grasp if you try to turn it into functional PHP code.

    Therefore, bearing in mind this possible inconvenience, I'm going to create a sample template class. As you’ll see in a few moments, this class will set up a concrete algorithm for formatting MySQL result sets.

    Having explained that, the signature that corresponds to this new template class is as follows: 

    // define abstract 'ResultTemplate' class
    abstract class ResultTemplate{
       // define template method (this is the algorithm setup by the base class)
       public final function displayFormattedResult($result){
         $output='';
         // get formatted number of rows
         if($this->getFormattedNumRows($result)==NULL){
           throw new Exception('Number of rows not available');
         }
         $output=$this->getFormattedNumRows($result).$this-
    >getFormattedRows($result);
         return $output;
       }
       // this sets the generic method to format number of database rows
       public function getFormattedNumRows($result){
         return NULL;
       }
       // this sets the generic method to format database rows
       public function getFormattedRows($result){}
    }

    After examining the definition of the above “ResultTemplate” class, definitely you’ll have to agree with me that things are getting really interesting. As you can see, this template class has been defined as abstract, but it also presents an important method called “displayFormattedResult().”

    Logically, the referenced method is responsible for setting up a specific algorithm for returning to calling code not only a string of formatted data sets, but the number of rows returned by the corresponding SELECT statement.

    Besides, it’s worthwhile to mention here the existence of two other methods, named “getFormattedNumRows()” and “getFormattedRows()” respectively. These methods are used in conjunction to display the aforementioned formatted data sets.

    Nonetheless, the signature of the previous “ResultTemplate” class demonstrates in a nutshell how the template pattern works, since this class defines an algorithm concretely (not abstractly), which will eventually be used by the respective child classes. Pretty simple to grasp, isn’t it?

    Okay, at this stage you already learned how the prior template class was built, but there are a couple of missing elements in this schema. Since the template in question is seated on top of the hierarchy of classes, it’s necessary to create some subclasses so that you can see how they interact with each other.

    As you may have guessed, these subclasses will be created in the section to come, so keep reading to learn more.



     
     
    >>> More PHP Articles          >>> More By Alejandro Gervasio
     

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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