PHP
  Home arrow PHP arrow Page 2 - 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 - The first step within the development process: coding form element classes
    ( Page 2 of 5 )

    A natural place to start building a form element factory is by coding the required classes to render each element. Maybe this sounds like common sense, but in fact it's a logical starting point within the development process. As you'd expect, writing the code for each form element doesn't present major difficulties due to its simplicity, but since we're dealing with many components, the task will be rather long.

    A brief disclaimer is needed here: it's often a better solution to define a base class that contains the methods and properties shared by any form element, and then derive the required specific subclasses. However, in this case I won't stick to this method, simply to make the code a little more abbreviated and demonstrate the polymorphism of objects. Anyway, by the end of this series you'll have the knowledge to implement your own version of the form application.

    Having said that, let's list the class source code for each form element. The first class renders a text input box, and looks like this:

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

    As I explained in the previous article, the above class generates the (X)HTML markup to display a regular input text box, which is returned by the "getHTML()" method. Let's move on to listing the class responsible for building password fields:

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

    Next, here is the list for hidden fields, file upload elements and submit images:

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

    Okay, sit tight just a little longer and jump into the next few lines. As you might guess, I'll show the remaining classes to get the form element classes completed.



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