PHP
  Home arrow PHP arrow Page 4 - 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? 
Google.com  
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 - Understanding how the abstract factory pattern works
    ( Page 4 of 4 )

    In order to understand more easily how diverse web form objects can be instantiated in accordance with a predefined context, below I set up an educational example. It shows in a step-by-step format how to display a few "required" and "normal" form elements, in consonance with a given instance of a factory class.

    The source code that corresponds to the example in question is as follows:

    try{
       
    // instantiate 'RequiredFormElementFactory' object
       // (works in the 'required' context)
      
    $factoryInstance=new RequiredFormElementFactory();
      
    $reqInputBox=$factoryInstance->createInputBox();
      
    $reqRadioButton=$factoryInstance->createRadioButton();
       
    $reqCheckBox=$factoryInstance->createCheckBox();
      
    // displays required form elements
      
    echo '<form>';
      
    echo $reqInputBox->display();
      
    echo $reqRadioButton->display();
      
    echo $reqCheckBox->display();
      
    echo '</form>';
      
    // instantiate 'NormalFormElementFactory' object
       // (works in the 'normal' context)
      
    $factoryInstance=new NormalFormElementFactory();
      
    $inputBox=$factoryInstance->createInputBox();
       
    $radioButton=$factoryInstance->createRadioButton();
      
    $checkBox=$factoryInstance->createCheckBox();
      
    // displays normal form elements
      
    echo '<form>';
       
    echo $inputBox->display();
      
    echo $radioButton->display();
      
    echo $checkBox->display();
      
    echo '</form>';
    }
    catch(Exception $e){
      
    echo $e->getMessage();
      
    exit();
    }

    Certainly, you must agree with me that the previous example is indeed demonstrative with reference to showing the functionality of the abstract factory pattern! As you can see, the above script first creates an instance of the "RequiredFormElementFactory" class, and then displays a simple web form, which contains a few "required " form elements. Logically, this is because this concrete factory makes sure that all the instantiated objects belongs to the "required" context.

    Next, the example continues doing its thing and creates a new instance of the "NormalFormElementFactory" class, which not surprisingly is tasked with spawning form objects that belong to the "normal" context. Pretty good, right?

    As you hopefully learned, in all cases, the concrete factories are completely responsible for making sure that the correct types of objects are returned to client code.

    As with all of my articles on web development with PHP, I suggest that you experiment with using and modifying all the classes that I developed here, so you can acquire a better background on how this design pattern works.

    Final thoughts

    That's all for the moment. In this three-part series, I provided you with a neat set of illustrative examples on how to build and use abstract factory classes with PHP 5.

    Although it's more probable that you'll use only singletons and concrete factories during the development of object-oriented applications, there will be situations where you'll need to use abstract factory classes to create objects that must exist only in a predefined context.

    See you in the next PHP development tutorial!



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

       

    PHP ARTICLES

    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - 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





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek