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.