HomePHP Page 2 - Generating Web Pages with the Flyweight Pattern in PHP 5
Building dynamic web page elements - 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.
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.