PHP
  Home arrow PHP arrow Page 5 - 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 - Instantiating form element objects: a simple use of the form element factory
    ( Page 5 of 5 )

    Creating form element objects through the form element factory is a fairly straightforward process. Object instantiation is handled internally behind the interface of the class, so the code looks compact and readable. Let's start factoring some form element objects, to clarify the usage of the factory:

    $textinput=formElementFactory::createElement
    ('textinput',array
    ('name'=>'fname','maxlength'=>'20'));

    $passwordinput=formElementFactory::createElement
    ('passwordinput',array
    ('name'=>'password','maxlength'=>'20'));

    $hiddeninput=formElementFactory::createElement
    ('hiddeninput',array
    ('name'=>'hiddendata','value'=>'default'));

    $imageinput=formElementFactory::createElement
    ('imageinput',array
    ('name'=>'imgdata','src'=>'subimage.gif'));

    $fileinput=formElementFactory::createElement
    ('fileinput',array('name'=>'upload','value'=>'1'));

    The above snippet simply creates several form objects. Note that object instantiation is performed behind the scenes, each time the "createElement()" method is invoked with the proper parameters. Since this method is static, it's possible to call it outside the class context by using the (::) resolution scope operator.

    If you think that the above examples are not complete, here is code to instantiate the rest of form element objects:

    $radiobutton=formElementFactory::createElement
    ('radiobutton',array
    ('name'=>'radio1','value'=>'1','checked'=>'true'));

    $textarea=formElementFactory::createElement
    ('textarea',array('name'=>'comments','value'=>'fill
    this field'));

    $checkbox=formElementFactory::createElement
    ('checkbox',array
    ('name'=>'checkbox1','value'=>'1','checked'=>'true'));

    $button=formElementFactory::createElement
    ('button',array('name'=>'button1','value'=>'button'));

    $submitbutton=formElementFactory::createElement
    ('submitbutton',array
    ('name'=>'send','value'=>'Send'));

    $resetbutton=formElementFactory::createElement
    ('resetbutton',array
    ('name'=>'reset','value'=>'Reset'));

    $selectbox=formElementFactory::createElement
    ('selectbox',array
    ('name'=>'select','size'=>'1','options'=>array
    ('1'=>'value1','2'=>'value2')));

    Finally, let's implement a basic loop, for roughly rendering some objects:

    // make array with form element objects
    $formElements=array($textinput,$passwordinput,$hiddeninput,$radiobutton,
     $textarea,$checkbox,$submitbutton);
    // display form elements
    foreach($formElements as $element){
      echo $element->getHTML().'<br />';
    }

    After executing the above snippet, what you get is a crude visual presentation of the form elements included in the array. Of course, a more polished look is highly desired, but for the moment, let's settle for the elements' rendering process of this example.

    As you can see, the defined "formElementFactory" class is doing its job quite well, despite its simplicity. Definitely, we've stepped forward to use the Factory Pattern within the form creation process, while the overall code has been maintained at a simple level.

    Notice the evident separation between object instantiation and the rest of client code. There are no "new" constructs that indicate where the objects are created. The whole process is handled by the internals of the "formElementFactory" class.

    There are still many aspects that need to be improved within the application, but you should remember that this is merely the beginning. The logic of the factory class must be modified, in order to get a higher level of abstraction to instantiate form objects, as well as get more control over visual presentation.

    For many of these features to work, you'll have to wait for the next part of the series. However, these are just a few ideas to get you started using the Factory pattern in real applications.

    Wrapping up

    Through this article, I've exposed the basics for implementing the Factory pattern in a real situation: building object-oriented forms. To fit this objective, a form element factory class has been defined, by showing that object instantiation can be completely decoupled from the client code, so a greater level of abstraction for rendering web-based forms is obtained within a program.

    Over the next part of the series, I'll add some features to the factory class, in order to encapsulate element rendering code within its structure. Meanwhile, play with the code and study its functionality. See you in the next part!



     
     
    >>> 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