PHP
  Home arrow PHP arrow Page 3 - Using Abstract Factory Classes in PHP 5 to Work with Online Forms
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

Using Abstract Factory Classes in PHP 5 to Work with Online Forms
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 9
    2007-02-07


    Table of Contents:
  • Using Abstract Factory Classes in PHP 5 to Work with Online Forms
  • Defining an abstract form element factory class
  • Creating context-driven form objects
  • Understanding how the abstract factory pattern works

  • 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


    Using Abstract Factory Classes in PHP 5 to Work with Online Forms - Creating context-driven form objects
    ( Page 3 of 4 )

    As you learned before, each one of the concrete factories defined previously exposes a "$context" property, which not surprisingly indicates the context in which different form objects must be created. So, taking into account this crucial feature that corresponds to the above mentioned classes, I'm going to define the first set of form elements classes. This set is tasked with rendering "required" input and form boxes, in addition to radio buttons.

    Given that, take a look at the following code listing. It shows the respective definitions for these brand new classes. They are as follows:

    // define 'RequiredInputBox' class
    class RequiredInputBox{
      
    public function display(){
        
    return '<input type="text" required="true" size="25" />';
     
    }
    }

    // define 'RequiredRadioButton' class
    class RequiredRadioButton{
      
    public function display(){
        
    return '<input type="radio" required="true" />';
      
    }
    }

    // define 'RequiredCheckBox' class
    class RequiredCheckBox{
      
    public function display(){
        
    return '<input type="checkbox" required="true" />';
      
    }
    }

    As you can see, the logic implemented by the three classes shown above is extremely easy to grasp, since all that these classes do is generate the required markup for creating mandatory form elements.

    Obviously, this condition is clearly demonstrated by adding to the markup in question, a "required" custom attribute, in this way indicating that all these form objects must work in a predefined "required" context.

    Now that I've shown you the signatures for all the classes that render "required" web form elements, please have a look at the code for the following classes, which are responsible for displaying "normal" (non-required) radio buttons, as well as input and check boxes.

    These classes look like this:

    // define 'NormalInputBox' class
    class NormalInputBox{
      
    public function display(){
        
    return '<input type="text" size="25" />';
      
    }
    }

    // define 'NormalRadioButton' class
    class NormalRadioButton{
      
    public function display(){
        
    return '<input type="radio" />';
      
    }
    }

    // define 'NormalCheckBox' class
    class NormalCheckBox{
      
    public function display(){
        
    return '<input type="checkbox" />';
      
    }
    }

    In this case, the above defined classes implement the logic necessary for creating non-required web form elements. Not surprisingly, this process is nearly identical to the one used for creating required form controls, but the "required" custom attribute has been completely removed from the pertinent markup.

    All right, at this stage I'm sure that you should have a pretty good idea of how all the classes that I defined a few lines before can be put to work together to demonstrate the functionality offered by the abstract factory pattern.

    Do you still need additional assistance to understand the topic? Okay, don't worry, because in the last section of this article, I'm going to develop an illustrative example, where you'll see how different form objects are instantiated by the corresponding concrete factories, by using only the context to which they belong.

    Want to see how this process will be performed? Go ahead and read the next few lines. I'll be there, waiting for you.



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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





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