PHP
  Home arrow PHP arrow Page 3 - Factoring Content Boxes with the Factory Pattern 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? 
Google.com  
PHP

Factoring Content Boxes with the Factory Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2007-07-09


    Table of Contents:
  • Factoring Content Boxes with the Factory Pattern in PHP 5
  • Factoring web page content boxes
  • Building different types of content boxes
  • Displaying web page content boxes via the factory pattern

  • 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


    Factoring Content Boxes with the Factory Pattern in PHP 5 - Building different types of content boxes
    ( Page 3 of 4 )

    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
     

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek