PHP
  Home arrow PHP arrow Page 3 - Factoring Content Boxes with the Facto...
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 
Moblin 
JMSL Numerical Library 
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

Factoring Content Boxes with the Factory Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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


    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


       · In this final part of the series, the popular factory pattern is used to create...
       · Short and Simple! Thanks!However, I would like to point out that since all...
       · First off, I'd like to thank you for commenting on my PHP article. Now, with regard...
       · Thank you for commenting on my PHP article, as well as for all the useful sugestions...
     

       

    PHP ARTICLES

    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP
    - PHP Networking
    - Validating Web Forms with the Code Igniter P...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT