#native_company# #native_desc#
#native_cta#

Using the Factory Pattern in PHP Applications

By Octavia Andreea Anghel
on February 23, 2011

In this article you will learn how to use the factory pattern design in your PHP application to construct different objects for interrogating a database (books) and displaying the records from a table (bookstore). You’ll also learn how to display a randomized array of strings and colors, and how to display a randomized array of strings and some associated pictures into an HTML table.
The essence of the factory method pattern is to “define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses.” (source: Wikipedia).
The factory pattern is a design pattern which is used in software development to encapsulate the processes used for creating objects. The factory pattern also allows for the instantiation of objects at runtime, as you will see in the below applications of this article. It is called a Factory Pattern because it is responsible for “manufacturing” an object — you’ll also see this below.
A factory method has a distinct name. In principle, a factory pattern can be used when: the creation of the object precludes reuse without significantly duplicating code, the creation of the object requires access to information or resources not appropriate to contain within the composing object, the lifetime management of created objects needs to be centralized to ensure consistent behavior.

Factory Pattern Method for PHP User Login Application

The polymorphism mechanism and the use of base class is really the center of OOP, but at some level, a concrete instance of the base class’s subclasses should be created. This is usually done using the factory pattern; a Factory class has a static method which receives some input and according to that input, it decides what class instance to create (usually a subclass).
Factory classes are useful when you need a complicated process for constructing the object, when the construction needs a dependency that you do not want for the actual class, and when you need to construct different objects.
To better understand when the factory pattern should be used, let’s take a real example. Suppose that on a website, different kinds of users can log in. Some are guests, some are regular customers, and others are administrators. Normally, you would have a base class User and have three subclasses: GuestUser, CustomerUser , and AdminUser . The base class, User, and its subclasses would contain methods that give information about the user (for example, permissions on what they can access on the website and their personal preferences). So, this is the moment when the factory pattern design pattern gets involved: the elegant way for you to write your Web application is to use the base class User as much as possible, so that the code would be generic and that it would be easy to add additional kinds of users when the need arises.
In the first application of this article you will see how the polymorphism mechanism works using two classes, RandomMessage and messageCreator. The base class is RandomMessage and the factory class is messageCreator. As you may notice, the messageCreator factory class contains the createMessage method which returns a new instance of the RandomMessage class. To output the result you should create an instance of the factory method, messageCreator, and use the getMessage and getColor from the base class, RandomMessage, to list the results.
Below you can see the listing and its result:

//Creating the RandomMessage class containing a function to randomize the 

//messages and colors arrays and two functions that returns the results class RandomMessage { var $messages; var $colors; function RandomMessage() { // Declaring the array of messages $this->messages=array( 'This is a factory pattern application!', 'The first application of this article!' ); // Declaring the array of colors $this->colors=array( '#FF33CC','#D6008F' ); // Shuffle the messages srand ((float)microtime()*1000000); shuffle ($this->messages); // Shuffle the colors srand ((float)microtime()*1000000); shuffle ($this->colors); } //Return the randomized message function getMessage() { return $this->messages[0]; } //Return the randomize color function getColor() { return $this->colors[0]; } } //Defining the factory class that contains the factory method class messageCreator { // The factory method... function & createMessage () { // Return a new instance of RandomMessage class return new RandomMessage(); } } //Creating an instance of the factory method, messageCreator, from above $messageMaker=new messageCreator; //Outputs the result for ( $i=0; $i<5; $i++ ) { $randomMessage=& $messageMaker->createMessage(); $color = $randomMessage->getColor(); echo ( ''.$randomMessage-> getMessage().' ' ); } ?>

Two of the result outputs of the above application are listed below:

This is a factory pattern application!
The first application of this article!
This is a factory pattern application!
The first application of this article!
The first application of this article!

This is a factory pattern application!
This is a factory pattern application!
This is a factory pattern application!
The first application of this article!
This is a factory pattern application!

You may notice that not only the messages are randomized but also the colors.