Home arrow PHP arrow Page 2 - Using Abstract Factory Classes in PHP 5 to Work with Online Forms

Defining an abstract form element factory class - PHP

Any PHP developer who has worked with pattern-based programming in PHP for a while knows that the abstract factory pattern is useful for building classes that return (to client code) objects whose type depend on the content where they're used. Welcome to the final installment of the series "Using abstract factory classes in PHP 5." If you're interested in learning the key concepts of this helpful pattern, this three-part series will teach you how to apply it by developing numerous educational examples.

  1. Using Abstract Factory Classes in PHP 5 to Work with Online Forms
  2. Defining an abstract form element factory class
  3. Creating context-driven form objects
  4. Understanding how the abstract factory pattern works
By: Alejandro Gervasio
Rating: starstarstarstarstar / 9
February 07, 2007

print this article



As I expressed in the introduction that you just read, I plan to define a group of concrete form element factories. These factories will be responsible for creating the correct types of form objects according to the context where these objects will be used.

Of course, before I proceed to defining this set of concrete factory classes, I must create an abstract form element factory class, in this way implementing the schema dictated by the referenced pattern.

As you'll see in a brief moment, the abstract factory class that I'm going to define will set up a generic interface for building simple input and check boxes, as well as radio buttons. Nonetheless, after creating the abstract factory class, I'm going to derive three subclasses from it. These classes will come in handy for spawning "required" and "non-required" form objects, therefore establishing the respective contexts where these objects will be utilized.

Okay, having said that, here are the corresponding signatures for the abstract and concrete factories classes that I discussed previously. Please take some time to examine their respective signatures:

// define abstract 'AbstractFormElementFactory' class
abstract class AbstractFormElementFactory{
abstract public function createInputBox();
abstract public function createRadioButton();
abstract public function createCheckBox();

// define concrete 'RequiredInputBoxFactory' class
class RequiredFormElementFactory extends AbstractFormElementFactory{
private $context='required';
public function createInputBox(){
return new RequiredInputBox();
public function createRadioButton(){
return new RequiredRadioButton();
public function createCheckBox(){
return new RequiredCheckBox();

// define concrete 'NormalInputBoxFactory' class
class NormalFormElementFactory extends AbstractFormElementFactory{
private $content='normal';
public function createInputBox(){
return new NormalInputBox();
public function createRadioButton(){
return new NormalRadioButton();
public function createCheckBox(){
return new NormalCheckBox();

As illustrated above, the first abstract factory class defines a generic interface. This interface is convenient for creating web form objects, including check and input boxes, as well as radio buttons. Nevertheless, if you pay attention to the pertinent concrete factories, you'll see that each one of them defines the context where the form objects will be utilized.

In this case in particular, the first factory will be responsible for creating web form objects that only work in a "required" context, while the second class will be capable of returning to client code, form objects that are functional in a "normal" (non-required) context. So far, the way that all the previous factory classes were defined is by far simple to understand, right?

Now, after having outlined generally the respective behaviors for each of the factory classes defined previously, it's time to show the corresponding definitions for all the form element classes. These are tasked with generating input and check boxes, and radio buttons as well, in consonance with a predefined context.

As usual, to see how all these brand new classes will be created, click on the link shown below and keep reading.

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