Home arrow PHP arrow Page 2 - The Singleton and Factory Patterns in PHP: a rendering-capable factory class

The Factory pattern in a real application: a quick look at the “formElementFactory” class - PHP

In this third part of the series, I will explain how to implement the Factory pattern in conjunction with the polymorphic characteristics of form objects. This will boost the functionality of the factory class by simplifying the process for rendering form elements.

  1. The Singleton and Factory Patterns in PHP: a rendering-capable factory class
  2. The Factory pattern in a real application: a quick look at the “formElementFactory” class
  3. A smarter factory: applying Polymorphism within the “formElementFactory” class
  4. Polishing the Page
By: Alejandro Gervasio
Rating: starstarstarstarstar / 11
November 16, 2005

print this article



As I’ve shown in my previous article, applying the Factory pattern to building object-based forms is fairly simple. As a result, I’ve developed a “formElementFactory” class that creates form objects and allows us to separate object instantiation from the rest of the program.

 To refresh what we’ve learned up to now, let’s have a look at the structure of the “formElementFactory” class and a possible implementation, assuming that each class for building form elements has already been defined. Here is its original definition:

abstract class formElementFactory{
    private function __construct(){}
    public static function createElement($type,$attributes=array()){
            throw new Exception('Invalid method parameters');
        // return a new form element object
        return new $type($attributes);

As you can see, the above class is fairly simple. The only relevant method that needs to be explained is “createElement()”, which does its work by returning an instance of the corresponding form object, according to the input passed to it. In this way, each time this static method is invoked with the correct parameters, that is the type of form object along with its parameters, a new object is returned for use within the program.

Normally, when the Factory pattern is used, you’ll find similar methods that are tasked with object instantiation, called “create()…” or “factor()..” something. You get the idea. All of them are descriptive names to define a method that performs some kind of object instantiation.

All right, having defined the factory class, a simple implementation looks like this:






Through the above snippet, I’ve created some simple form objects, such as text input boxes, or hidden fields, as a crude implementation for building a form. Once several objects are available, I’m able to display a few form elements by utilizing a regular “foreach” loop, as shown below:

// make array with form element objects
// display form elements
foreach($formElements as $element){
            echo $element->getHTML().'<br />';

Certainly, the above code shows how simply it is spawning several form objects and getting its (X)HTML code to render the form in a very basic way. As you’ll probably agree, there is not much control over the layout of each element, but the technique presents many advantages compared to writing forms in the usual way. So, returning to the problems that the factory class presents, if I introduce a few modifications within its source code, it’s possible to add form element rendering capabilities to the structure, by taking advantage of the polymorphic nature of each form object.

Perhaps this sounds rather confusing, so let’s jump into the next section to find out how it’s done.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: