PHP
  Home arrow PHP arrow Page 4 - The Singleton and Factory Patterns in PHP: Building a Form Generator Class
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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

The Singleton and Factory Patterns in PHP: Building a Form Generator Class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2005-12-07


    Table of Contents:
  • The Singleton and Factory Patterns in PHP: Building a Form Generator Class
  • Object-based form generation (continued): explaining the remaining class methods
  • A practical example: putting the “formGenerator” class to the test
  • The source code box: listing the full developed classes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    The Singleton and Factory Patterns in PHP: Building a Form Generator Class - The source code box: listing the full developed classes
    ( Page 4 of 4 )

    As I said before, here is the full list of the classes used over the series:

    /*
    ***************************************
    class definitions for form elements
    ***************************************
    */
    // class textinput
    class textinput{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="text" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class passwordinput
    class passwordinput{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="password" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class hiddeninput
    class hiddeninput{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="hidden" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class fileinput
    class fileinput{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="file" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class imageinput
    class imageinput{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="image" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class radiobutton
    class radiobutton{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="radio" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class checkbox
    class checkbox{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="checkbox" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class button
    class button{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="button" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class submitbutton
    class submitbutton{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="submit" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class resetbutton
    class resetbutton{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<input type="reset" ';
            foreach($attributes as $attribute=>$value){
                $this->html.=$attribute.'="'.$value.'" ';
            }
            $this->html.='/>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class textarea
    class textarea{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<textarea ';
            $textvalue='';
            foreach($attributes as $attribute=>$value){
                ($attribute!='value')?$this-
    >html.=$attribute.'="'.$value.'" ':$textvalue=$value;
            }
            $this->html=preg_replace("/\"? $/","\">",$this->html);
            $this->html.=$textvalue.'</textarea>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    // class selectbox
    class selectbox{
        private $html;
        public function __construct($attributes=array()){
            if(count($attributes)<1){
                throw new Exception ('Invalid number of attributes');
            }
            $this->html='<select ';
            $options='';
            foreach($attributes as $attribute=>$value){
                if($attribute!='options'){
                    $this->html.=$attribute.'="'.$value.'" ';
                }
                else{
                    foreach($value as $values=>$label){
                        $options.='<option value="'.$values.'">'.$label.'</option>';
                    }
                }
            }
            $this->html=preg_replace("/\"? $/","\">",$this->html);
            $this->html.=$options.'</select>';
        }
        public function getHTML(){
            return $this->html;
        }
    }
    /*
    *****************************************
    abstract formElementFactory class
    *****************************************
    */
    // abstract class formElementFactory
    // this class is abstract thus cannot be instantiated
    abstract class formElementFactory{
        private function __construct(){}
        public static function createElement($type,$attributes=array
    ()){
            if(!class_exists($type)||!is_array($attributes)){
                throw new Exception('Invalid method parameters');
            }
            // instantiate a new form element object
            $formElement=new $type($attributes);
            // return HTML of form element code
            return $formElement->getHTML();
        }
    }
    /*
    **************************************
    // formGenerator class definition
    **************************************
    */
    class formGenerator{
        // data member declaration
        private $elements=array(); // array of form elements
        private $output=''; // dynamic output
        private $elementHeader=''; // element header
        private $elementFooter='<br />'; // element footer
        private $name='theform'; // form name
        private $method='post'; // form method
        private $action; // form action
        // constructor
        public function __construct($elements=array()){
            if(count($elements)<1){
                throw new Exception('Invalid number of elements');
            }
            // data member initialization
            $this->elements=$elements;
            $this->action=$_SERVER['PHP_SELF'];
        }
        // create form code
        public function createForm(){
            $this->output.='<form name="'.$this->name.'"
    action="'.$this->action.'" method="'.$this->method.'">';
            foreach($this->elements as $element=>$attributes){
                // call the abstract class formElementFactory
                $this->output.=$this-
    >elementHeader.formElementFactory::createElement
    ($element,$attributes).$this->elementFooter;
            }
            $this->output.='</form>';
        }
        // add form part
        public function addFormPart($html='<br />'){
            $this->output.=$html;
        }
        // add element header
        public function addElementHeader($header){
            $this->elementHeader=$header;
        }
        // add element footer
        public function addElementFooter($footer){
            $this->elementFooter=$footer;
        }
        // set form name
        public function setName($name){
            $this->name=$name;
        }
        // set form action
        public function setAction($action){
            $this->action=$action;
        }
        // set form method
        public function setMethod($method){
            if($method!='post'&&$method!='get'){
                throw new Exception('Invalid form method');
            }
            $this->method=$method;
        }
        // get dynamic form output
        public function getFormCode(){
            return $this->output;
        }
    }

    That’s about it. Hopefully you’ll have some fun studying the code and tweaking it to fit your personal needs. Also, so as to not make the code excessively long, I’ve not listed the classes suitable for PHP 4, but you may want to take a look at them in the previous article. A final note: all of the examples have been tested on PHP 4.3.4 and PHP 5.0.4. respectively. Happy coding!



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

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





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