Home arrow PHP arrow Page 3 - Generating Web Pages with the Flyweight Pattern in PHP 5

Defining a flyweight factory class - PHP

Unnecessary and balanced instantiation of PHP classes are issues that can be easily solved by using the flyweight design pattern. If you want to learn more about it, you should start reading this article. Welcome to the final part of the series “Using the flyweight pattern with PHP 5.” As you may have guessed, this series walks you through the implementation of this helpful pattern with PHP, and shows you how to apply it in concrete cases.

  1. Generating Web Pages with the Flyweight Pattern in PHP 5
  2. Building dynamic web page elements
  3. Defining a flyweight factory class
  4. Putting the flyweight factory class to work
By: Alejandro Gervasio
Rating: starstarstarstarstar / 6
March 05, 2007

print this article



To develop a web page generator system capable of maintaining balance with the number of DIV objects created across the application, I must first define a flyweight class. This class will be provided with the capacity to return only three DIVs to client code, identified as "diva","divb", and "divc" respectively.

Naturally, in accordance with the logic that stands behind the flyweight pattern, this factory class will refuse any attempts to create more DIV objects, in this manner keeping in equilibrium the number of objects used by the web page generator application.

Now that you know how this flyweight class is going to work, I'd like you to pay attention to its signature, which is listed below:

// define 'FlyweightDivElementFactory' class
class FlyweightDivElementFactory{
   private $divElements=array();
   public function __construct(){
   // return to calling code only three DIVS
   public function getDivElement($divId){
       throw new Exception('Invalid ID attribute for DIV
       $this->divElements[$divId]=new DivElement($divId);
     return $this->divElements[$divId];

You can see that the above flyweight factory class has one relevant method, called "fetchDivElement()," which is responsible for controlling the number of DIV objects that will be returned to client code. As one would expect, if an invalid $divID argument is passed to this method, an exception will be triggered, and program's execution will be stopped.

At this stage, hopefully you'll have a much better idea of how the previous flyweight factory class does its business. So, provided that my assumption here is correct, it's time to move forward and see how this class can be put to work to generate dynamic web pages that include only three DIV elements.

The experience sounds interesting, therefore if you wish to learn how the previous flyweight class will be used to create web documents on the fly, please jump into the following section. Don't worry, since I'll be there, waiting for you.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates


Dev Shed Tutorial Topics: