PHP
  Home arrow PHP arrow Page 4 - 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 Factory Pattern in action: developing the "formElementFactory" class
    ( Page 4 of 5 )

    In general terms, the Factory pattern allows you to separate object instantiation from the rest of the client code. The immediate benefit of applying this pattern is that you're able to create an instance of a class within a program that returns a specific object, each time it's invoked with the proper parameters (called input), without being concerned about how the object is instantiated. Object instantiation is handled internally by the class, which is called a "factory," while the object instantiated is the "product."

    Now, allow me to explain how I translate the above theory into a functional class. First, I'll define a simple abstract class, called "formElementFactory," which returns an instance of a form element object, according to the input passed to it. To clarify things, here is the first definition for this class:

    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 class is not complex. I've defined only two methods: the empty private constructor and the public static "createElement()" method. Certainly, this method takes care of the proper object instantiation and accepts two parameters: $type and the $attributes array. The first one represents the type of object to be instantiated, while the second means its attributes.

    The line below:

    return new $type($attributes);

    carries out the object instantiation. Simple and powerful, eh?

    Also, the class expects the input to conform to a specific type of object. I could go even further in terms of abstraction by refactoring the class, so it can accept an object with no explicit type. In that case, I'd be applying the Abstract Factory pattern, which at the moment is out of the scope of the article.

    By this point, I hope that you're wondering why I declared the class as abstract. The reason is simple. An abstract class cannot be instantiated, thus any attempt to do so will result in a fatal error. Declaring the class abstract is an efficient way to use its methods without dealing with its instantiation. This means that there's no class instance, but the methods are accessible from outside its context.

    PHP5 makes it really easy to work with abstract classes. Unfortunately PHP4 doesn't support them, but there is a workaround to emulate this structure, generally by coding a "die()" statement within the constructor.

    The rest of the code speaks for itself. Parameter validation is performed at a basic level by checking whether the type of object exists, as well as whether the $attributes variable is an array. Checking code should be modified if form element classes reside in a different location.

    Now that we've analyzed in detail the factory class, let's implement some examples to evaluate its functionality.



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