PHP
  Home arrow PHP arrow Page 2 - Generating Web Pages with the Flyweigh...
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 
Mobile Linux 
App Generation ROI 
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

Generating Web Pages with the Flyweight Pattern in PHP 5
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 6
    2007-03-05

    Table of Contents:
  • Generating Web Pages with the Flyweight Pattern in PHP 5
  • Building dynamic web page elements
  • Defining a flyweight factory class
  • Putting the flyweight factory class to work

  • 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


    Generating Web Pages with the Flyweight Pattern in PHP 5 - Building dynamic web page elements


    (Page 2 of 4 )

    Since in the introduction that you just read I stated that I was going to demonstrate how to use the flyweight pattern for building dynamic web pages, my first step is to define a basic class, called "DivElement," which will be tasked with displaying DIVs on a web document.

    Once you have grasped the logic followed by this class, I'll create a flyweight class which will be responsible for keeping the number of DIV objects limited to a specific number.

    But for the moment, pay attention to the signature of the following "DivElement" class, which looks like this:

    // define 'DivElement' class
    class DivElement{
       private $id;
       private $class;
       private $content;
       public function __construct($id='defaultid',
    $class='defaultclass',$content='Default content for DIV goes
    here.'){
         if(!preg_match("/[a-zA-Z]+/",$id)){
           throw new Exception('Invalid value for ID property!');
         }
         if(!preg_match("/[a-zA-Z]+/",$class)){
           throw new Exception('Invalid value for name property!');
         }
         if(!$content){
           throw new Exception('Invalid content for div element!');
         }
         $this->id=$id;
         $this->class=$class;
         $this->content=$content;
       }
       // fetch (X)HTML markup of div element
       public function getHTML(){
         return '<div id="'.$this->id.'" class="'.$this-
    >class.'">'.$this->content.'</div>';
       }
    }

    As you'll realize, the above "DivElement" class is quite simple to follow. In consonance with the concepts that I deployed a few lines above, it's tasked with displaying a generic containing DIV on a web document. So far, this isn't rocket science, right?

    Logically, a possible implementation for the previous class, to build a simple web page, could be coded as follows:

    // example building a web page using the 'DivElement' class
    try{
       // instantiate DivElement objects
       $divA=new DivElement('diva','clsa','Content for DIV A goes
    here.');
       $divB=new DivElement('divb','clsb','Content for DIV B goes
    here.');
       $divC=new DivElement('divc','clsc','Content for DIV C goes
    here.');
       // display web page
       echo '<html><head><title>Sample Web Page</title></head><body>'.$divA->getHTML().$divB->getHTML
    ().$divC->getHTML().'</body></html>';
    }
    catch(Exception $e){
       echo $e->getMessage();
       exit();
    }

    As you can see, the prior script shows how to use the "DivElement" class that I defined previously to create a sample web document that contains only three DIVs. The example is very easy to grasp, so you shouldn't have any problems understanding how it works.

    However, the aforementioned "DivElement" class apparently has nothing to do with applying the flyweight pattern. Well, not so fast. Suppose that you want to develop an application that generates dynamic web pages with a few DIV objects, but at the same time, you need to keep the amount of instantiated DIVs limited to a specific number. How can this be done?

    Yes, you guessed right! The answer to the above question is obviously using the flyweight pattern. Doing so, it's feasible to create a web page generator system that not only constructs web documents on the fly, but keeps the number of DIV objects required to build the web pages in question completely under our control.

    Therefore, taking into account that the flyweight pattern now starts to play a relevant role, in the course of the section to come, I'm going to show you how to create a flyweight class which will be capable of controlling (and balancing, by the way), the number of instances that correspond to the aforementioned "DivElement" class.

    Want to find out how this brand new class will be defined? Visit the next section and keep reading.

    More PHP Articles
    More By Alejandro Gervasio


       · In this final article of the series, the flyweight pattern is used to develop a...
       · Sorry, but having read the first part "Introducing the Flyweight Pattern with PHP...
       · Hello Gregor,Thank you for commenting on my PHP article. Now, with reference to...
       · this article is not about the flyweight pattern at all, consider reading about them...
       · Thank you for commenting on my PHP article about implementing the flyweight pattern...
     

       

    PHP ARTICLES

    - Working With Different Namespaces in PHP 5
    - User Management Explained: Overview
    - Using Namespaces in PHP 5
    - Database Security: Guarding Against SQL Inje...
    - Building a Modular Exception Class in PHP 5
    - Database and Password Security for Web Appli...
    - Handling MySQL Data Set Failures in PHP 5
    - Building Site Registration for Web Applicati...
    - Intercepting Customized Exceptions in PHP 5
    - Securing Your Web Application Against Attacks
    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter





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