Home arrow PHP arrow Page 3 - Factoring Content Boxes with the Factory Pattern in PHP 5

Building different types of content boxes - PHP

Working with factory classes is a rather common process for many PHP developers who build object-based applications on a frequent basis. If you want to find out how to make this helpful class work for you, then this group of articles might be what you need. Welcome to the final installment of the series that began with "Using the Factory Pattern in PHP 5." In three parts, this series goes through the key concepts of implementing the factory pattern with PHP, and complements theory with numerous and educational code samples.

TABLE OF CONTENTS:
  1. Factoring Content Boxes with the Factory Pattern in PHP 5
  2. Factoring web page content boxes
  3. Building different types of content boxes
  4. Displaying web page content boxes via the factory pattern
By: Alejandro Gervasio
Rating: starstarstarstarstar / 4
July 09, 2007

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I stated in the section that you just read, to complete the schema dictated by the factory pattern, it's necessary to define some additional classes. These classes will display the complete set of content boxes that you learned previously.

Here are the corresponding signatures for these new classes. Take a look at them, please:

// define abstract 'ContentBox' class
abstract class ContentBox{
   private $title;
   private $content;
   abstract public function __construct($title,$content);
     abstract public function display();
}

// define concrete 'GreyContentBox' class
class GreyContentBox extends ContentBox{
   public function __construct($title,$content){
     if(!$title){
       throw new Exception('A title for the content box must be
provided.');
     }
     if(!$content){
       throw new Exception('The content for the box must be
provided.');
     }
     $this->title=$title;
     $this->content=$content;                       
   }
   public function display(){
     return '<div class="greybox"><h2>'.$this-
>title.'</h2><p>'.$this->content.'</p></div>';
   }
}

// define concrete 'BlueContentBox' class
class BlueContentBox extends ContentBox{
   public function __construct($title,$content){
     if(!$title){
       throw new Exception('A title for the content box must be
provided.');
     }
     if(!$content){
       throw new Exception('The content for the box must be
provided.');
     }
     $this->title=$title;
     $this->content=$content;                       
   }
   public function display(){
     return '<div class="bluebox"><h2>'.$this-
>title.'</h2><p>'.$this->content.'</p></div>';
   }
}

// define concrete 'YellowContentBox' class
class YellowContentBox extends ContentBox{
   public function __construct($title,$content){
     if(!$title){
       throw new Exception('A title for the content box must be
provided.');
     }
     if(!$content){
       throw new Exception('The content for the box must be
provided.');
     }
     $this->title=$title;
     $this->content=$content;                       
   }
   public function display(){
     return '<div class="yellowbox"><h2>'.$this-
>title.'</h2><p>'.$this->content.'</p></div>';
   }
}

As you can see, the three concrete classes shown above present the same "display()" method, which comes in useful for displaying a concrete type of content box. In addition, the classes' constructor accepts some simple input parameters, like the title and contents that will be housed by the box in question. Quite simple, right?

All right, I believe that at this point I provided you with all the PHP classes required to dynamically display a bunch of content boxes on any web page. Since I know you may want to see how all these components can be linked with each other, in the section to come I'm going to develop an example which hopefully will show you the functionality of the factory pattern in a real world situation.

To learn more about how this practical example will be developed, click on the link below and read the next few lines.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap

Dev Shed Tutorial Topics: