PHP
  Home arrow PHP arrow Page 4 - The Singleton and Factory Patterns in ...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
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: 4 stars4 stars4 stars4 stars4 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · The final part of this series goes trough the makings of a form generator class, by...
       · Just wondering how you would have a web-designer and a developer team-develop the...
       · Well, thank you for the comments on my article, since that proves it was quite...
       · Widgets would be my suggestion as well. Just ensuring that those less versed in...
       · Thanks to you for the quick and positive response! Also, I'm glad to know that you...
       · Excellent write up Alejandro. May I ask for the reason this last part is written for...
       · Hello Matthijs,Thank you for the compliments on this PHP article series. It's...
       · Hi Alejandro,Thanks for your reply. Your suggestion did solve the problem I had!...
       · Hi Matthijs,Good to know that your problem is now fixed up. Of course it's...
       · That's really helpful Alejandro. I'll take your suggestions and build something. I...
       · Thank you Matthijs. I'm happy to know that my suggestions were helpful to you.My...
       · Hi Alejandro,Thinking about patterns, and comparing your earlier articles about...
       · Hello Matthijs,Well, it's not a hard question, but perphaps it's a little long...
       · Your comments are certainly helpful. And thanks for the links. I did see the site...
       · Hi, Mathjis. It's good to know that my answers were useful to you, as well as the...
       · Thanks for a great article it's gone along way in selling the benefits of OOP in...
       · Thank you for the kind comments on my article. I really appreciate your feedback....
       · Many thanks for the quick reply Alejandro. I'll give that a go - it should give some...
       · Thanks - worked a treat. I might try using this pattern for field validation as...
       · Thank you. Also, I'm glad to know that this form generator class has been useful to...
       · Thank you for the comments. Yes, adding form validation routines would be a nice...
       · Your form generator example must be excellent, as it was very straightforward for me...
       · Hello - Your form script is great - I'm just wondering how you would include a users...
       · Thank you I was having the same issue with this code.
       · Hi Regan,Thanks for the kind words on my PHP article. There’s a few simple ways...
     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway