PHP
  Home arrow PHP arrow Page 3 - The Singleton and Factory Patterns in PHP: designing an object factory
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: designing an object factory
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 13
    2005-11-09


    Table of Contents:
  • The Singleton and Factory Patterns in PHP: designing an object factory
  • The first step within the development process: coding form element classes
  • Finishing the round: listing the rest of form element classes
  • The Factory Pattern in action: developing the "formElementFactory" class
  • Instantiating form element objects: a simple use of the form element factory

  • 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: designing an object factory - Finishing the round: listing the rest of form element classes
    ( Page 3 of 5 )

    Now that you have the code for input text boxes, hidden fields, file input fields and so forth, I'll finish writing the rest of the form element classes, that is, radio buttons, checkboxes, select boxes and text area fields, to name a few.

    Notice that most of element classes are similar in their source code. This might lead you to think that all of the classes can be unified into one generic class that would accept one parameter (the element's name), and accordingly return the corresponding (X)HTML code.

    While this method might be valid for reducing the length of code, it's preferable to keep each form element class as a separate entity within the application, allowing you to maintain a greater level of flexibility.

    Now, it's time to finish the coding round by listing the classes for the rest of the form elements. Here is the code for radio buttons, checkboxes, and buttons:

    // 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;
      }
    }

    Finally, below are the classes for rendering submit buttons, reset buttons, text areas and select boxes:

    // 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;
      }
    }

    By this point, most of the hard work has been done. Now that you have all of the form element classes defined, I can go one step further and start writing the form element factory. Thus, click below and keep reading.



     
     
    >>> 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 2 hosted by Hostway
    Stay green...Green IT