Home arrow PHP arrow 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.

TABLE OF CONTENTS:
  1. The Singleton and Factory Patterns in PHP: Working With Singletons
  2. Working with a single object instance: making the “formElementFactory” class a Singleton
  3. The previous step: defining form element classes in PHP4
  4. Ending up the coding round: defining the remaining form element classes
By: Alejandro Gervasio
Rating: starstarstarstarstar / 5
November 30, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: