HomePHP Page 2 - The Basics of Using the Factory Pattern in PHP 5
Developing some basic factory classes - PHP
If you need to create multiple objects that belong to the same family, you probably want to use the factory pattern. This three-part series takes a close look at using the factory pattern in PHP.
To start demonstrating the neat functionality provided by the factory pattern, I'll define a few basic factory classes whose primary task will consist of returning different types of array objects to client code according to the concrete specifications of a given programming environment.
As you'll see, these concrete factories will be capable of "factoring" array objects that will work with both numeric and associative indexes. But let's get away from theory for a moment and see how these brand new factory classes look. Their respective signatures are as follows:
// define abstract 'ArrayFactory' class abstract class ArrayFactory{ abstract public function createArrayObj($type); }
// define concrete factory to create numerically-indexed array objects class NumericArrayFactory extends ArrayFactory{ private $context='numeric'; public function createArrayObj($type){ $arrayObj=NULL; switch($type){ case "uppercase"; $arrayObj=new UppercasedNumericArrayObj(); break; case "lowercase"; $arrayObj=new LowercasedNumericArrayObj(); break; default: $arrayObj=new LowercasedNumericArrayObj(); break; } return $arrayObj; } }
// define concrete factory to create associative array objects class AssociativeArrayFactory extends ArrayFactory{ private $context='associative'; public function createArrayObj($type){ $arrayObj=NULL; switch($type){ case "uppercase"; $arrayObj=new UppercasedAssociativeArrayObj(); break; case "lowercase"; $arrayObj=new LowercasedAssociativeArrayObj(); break; default: $arrayObj=new LowercasedAssociativeArrayObj(); break; } return $arrayObj; } }
As demonstrated above, the previous concrete factory classes (excepting logically the first one, which has been declared abstract) implement the required business logic to create different types of array objects in accordance with the specifications of a given environment.
In consonance with the previous concept, the first factory class, which is called "NumericArrayFactory," is responsible for spawning three different types of numeric array objects. All of them are obviously conceived to work in a "numeric" context.
Now, concerning the definition of the second concrete factory, you can see that this one returns only associative array objects to client code, which logically must conform to the specifications of an "associative" environment.
Undeniably, after studying the respective signatures of the two previous factory classes, you'll realize that implementing the factory pattern with PHP is actually an easy-to-grasp process that can be performed with minor hassles.
So far, so good, right? At this stage I demonstrated how to build a couple of concrete factory classes that are tasked with spawning different types of array objects. So what's the next step? Well, as you learned before, these array objects obviously are spawned from a bunch of concrete classes, so in the following section I'm going to show you their respective signatures. You'll see more easily how the different classes used to implement the factory pattern are linked with each other.
To learn how these array-related classes will be defined, please click on the link below and keep reading.