HomePHP Page 3 - The Singleton and Factory Patterns in PHP: Working With Singletons
The previous step: defining form element classes in PHP4 - PHP
In this fourth part of the series covering the Singleton and Factory Design Patterns in PHP, we will discuss issues stemming from the fact that PHP 4 does not have an abstract class. Since we found it useful in the previous article to define the form element factory class as an abstract class, in this article we will discuss the process for making the form element factory class a Singleton, and how this serves our purposes.
Before working with the Singleton model of the form element factory, it’s best to define all of the form element classes, suitable for use in PHP4. Since they’re closely similar to their counterparts in PHP5, this shouldn’t present any difficulties. Here they are:
// class textinput class textinput{ var $html; function textinput($attributes=array()){ if(count($attributes)<1){ trigger_error('Invalid number of attributes'); exit(); } $this->html='<input type="text" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } function getHTML(){ return $this->html; } } // class password input class passwordinput{ var $html; function passwordinput($attributes=array()){ if(count($attributes)<1){ trigger_error('Invalid number of attributes'); exit(); } $this->html='<input type="text" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } function getHTML(){ return $this->html; } } // class hidden input class hiddeninput{ var $html; function textinput($attributes=array()){ if(count($attributes)<1){ trigger_error('Invalid number of attributes'); exit(); } $this->html='<input type="hidden" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } function getHTML(){ return $this->html; } } // class file input class fileinput{ var $html; function fileinput($attributes=array()){ if(count($attributes)<1){ trigger_error('Invalid number of attributes'); exit(); } $this->html='<input type="file" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } function getHTML(){ return $this->html; } } // class image input class imageinput{ var $html; function imageinput($attributes=array()){ if(count($attributes)<1){ trigger_error('Invalid number of attributes'); exit(); } $this->html='<input type="image" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } function getHTML(){ return $this->html; } } // class radio button class radiobutton{ var $html; function radiobutton($attributes=array()){ if(count($attributes)<1){ trigger_error('Invalid number of attributes'); exit(); } $this->html='<input type="radio" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } function getHTML(){ return $this->html; } } // class check box class checkbox{ var $html; function checkbox($attributes=array()){ if(count($attributes)<1){ trigger_error('Invalid number of attributes'); exit(); } $this->html='<input type="checkbox" '; foreach($attributes as $attribute=>$value){ $this->html.=$attribute.'="'.$value.'" '; } $this->html.='/>'; } function getHTML(){ return $this->html; } }
All right, let’s pause for a moment and relax. At this point, most of the form element classes has been listed, so the walk through the source code is almost finished. There are a few more classes to be defined yet, so keep reading.