Home arrow PHP arrow Page 5 - Working with Template Classes in PHP 5

Setting up a functional example - PHP

A template class is a base class with some special abilities. First, it concretely implements one or more algorithms; second, it can determine which subclasses should use these algorithms. This article is the first part of a two-part series that introduces you to the basic concepts of this design pattern.

TABLE OF CONTENTS:
  1. Working with Template Classes in PHP 5
  2. Creating a basic implementation of the template pattern
  3. Building a simple template class
  4. Completing the model imposed by the template pattern
  5. Setting up a functional example
By: Alejandro Gervasio
Rating: starstarstarstarstar / 18
March 19, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: