Home arrow PHP arrow Page 4 - The Singleton and Factory Patterns in PHP: designing an object factory

The Factory Pattern in action: developing the "formElementFactory" class - PHP

This article, the second part of a series, is focused on implementing the Factor Pattern by designing a form element factory.

TABLE OF CONTENTS:
  1. The Singleton and Factory Patterns in PHP: designing an object factory
  2. The first step within the development process: coding form element classes
  3. Finishing the round: listing the rest of form element classes
  4. The Factory Pattern in action: developing the "formElementFactory" class
  5. Instantiating form element objects: a simple use of the form element factory
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
November 09, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: