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