PHP
  Home arrow PHP arrow Page 5 - 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 - Setting up a functional example
    ( Page 5 of 5 )

    As I stated in the previous section, below I included a hands-on example that demonstrates how all the template classes that were built previously can be utilized in a pretty useful way. The sample script fetches a simple result set from a sample “USERS” database table, and then uses the different templates to format this data set.

    That being said, the corresponding code listing is as follows:

    try{
       // connect to MySQL
       $db=new MySQL(array
    ('host'=>'host','user'=>'user','password'=>
    'password','database'=>'mydatabase'));
       $result=$db->query('SELECT * FROM users');
       // instantiate 'ParagraphResultTemplate' object
       $parResultTemplate=new ParagraphResultTemplate();
       // display database formatted database result set
       echo $parResultTemplate->displayFormattedResult($result);
       /*
       displays the following (formatted as paragraphs):

       Number of rows: 10
       Record listing:
       1user1user1@domain.com
       2user2user2@domain.com
       3user3user3@domain.com
       4user4user4@domain.com
       5user5user5@domain.com
       6user6user6@domain.com
       7user7user7@domain.com
       8user8user8@domain.com
       9user9user9@domain.com
       10user10user10@domain.com
       */

                
       // instantiate 'DivResultTemplate' object
       $divResultTemplate=new DivResultTemplate();
       // run query against selected database
       $result=$db->query('SELECT * FROM users');
       // display database formatted database result set
       echo $divResultTemplate->displayFormattedResult($result);
       /*
       displays the following (formatted as DIV elements):

       Number of rows: 10
       Record listing:
       1user1user1@domain.com
       2user2user2@domain.com
       3user3user3@domain.com
       4user4user4@domain.com
       5user5user5@domain.com
       6user6user6@domain.com
       7user7user7@domain.com
       8user8user8@domain.com
       9user9user9@domain.com
       10user10user10@domain.com
       */

                
       // instantiate 'HeaderResultTemplate' object
       $headerResultTemplate=new HeaderResultTemplate();
       // run query against selected database
       $result=$db->query('SELECT * FROM users');
       // display database formatted database result set
       echo $headerResultTemplate->displayFormattedResult($result);
       /*
       displays the following (formatted as <h3> headers):

       Number of rows: 10
       Record listing:
       1user1user1@domain.com
       2user2user2@domain.com
       3user3user3@domain.com
       4user4user4@domain.com
       5user5user5@domain.com
       6user6user6@domain.com
       7user7user7@domain.com
       8user8user8@domain.com
       9user9user9@domain.com
       10user10user10@domain.com
       */
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As indicated above, all the template sub classes use the formatting algorithm defined by the parent to display a primitive MySQL data set using different (X)HTML tags. In the first case, database table rows are displayed as a group of paragraphs, while in the other two cases, the same rows are shown as a set of DIVs and <h3> headers respectively.

    Feel free to modify all the classes shown here. In this way you can develop your own examples, and eventually acquire a better background in how the template pattern works. You’ll have a good time, trust me!

    Final thoughts

    In this first part of the series, I introduced the key concepts concerning the implementation of the template pattern with PHP 5. Also, you may have noticed that this pattern is rather unusual, particularly because the base template class has considerable control over how the respective subclasses must work.

    In the next (and last) part of the series, I’ll be developing another instructive example where this pattern can be applied: the manipulation of file data via a template object. You won’t want to miss it!



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