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


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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