PHP
  Home arrow PHP arrow Page 2 - The Singleton and Factory Patterns in PHP: a rendering-capable factory class
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

The Singleton and Factory Patterns in PHP: a rendering-capable factory class
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 11
    2005-11-16


    Table of Contents:
  • The Singleton and Factory Patterns in PHP: a rendering-capable factory class
  • The Factory pattern in a real application: a quick look at the “formElementFactory” class
  • A smarter factory: applying Polymorphism within the “formElementFactory” class
  • Polishing the Page

  • 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: a rendering-capable factory class - The Factory pattern in a real application: a quick look at the “formElementFactory” class
    ( Page 2 of 4 )

    As I’ve shown in my previous article, applying the Factory pattern to building object-based forms is fairly simple. As a result, I’ve developed a “formElementFactory” class that creates form objects and allows us to separate object instantiation from the rest of the program.

     To refresh what we’ve learned up to now, let’s have a look at the structure of the “formElementFactory” class and a possible implementation, assuming that each class for building form elements has already been defined. Here is its original definition:

    abstract class formElementFactory{
        private function __construct(){}
        public static function createElement($type,$attributes=array()){
            if(!class_exists($type)||!is_array($attributes)){
                throw new Exception('Invalid method parameters');
            }
            // return a new form element object
            return new $type($attributes);
        }
    }

    As you can see, the above class is fairly simple. The only relevant method that needs to be explained is “createElement()”, which does its work by returning an instance of the corresponding form object, according to the input passed to it. In this way, each time this static method is invoked with the correct parameters, that is the type of form object along with its parameters, a new object is returned for use within the program.

    Normally, when the Factory pattern is used, you’ll find similar methods that are tasked with object instantiation, called “create()…” or “factor()..” something. You get the idea. All of them are descriptive names to define a method that performs some kind of object instantiation.

    All right, having defined the factory class, a simple implementation looks like this:

    $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'));

    Through the above snippet, I’ve created some simple form objects, such as text input boxes, or hidden fields, as a crude implementation for building a form. Once several objects are available, I’m able to display a few form elements by utilizing a regular “foreach” loop, as shown below:

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

    Certainly, the above code shows how simply it is spawning several form objects and getting its (X)HTML code to render the form in a very basic way. As you’ll probably agree, there is not much control over the layout of each element, but the technique presents many advantages compared to writing forms in the usual way. So, returning to the problems that the factory class presents, if I introduce a few modifications within its source code, it’s possible to add form element rendering capabilities to the structure, by taking advantage of the polymorphic nature of each form object.

    Perhaps this sounds rather confusing, so let’s jump into the next section to find out how it’s done.



     
     
    >>> 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek