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

Creating context-driven form objects - 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 you learned before, each one of the concrete factories defined previously exposes a "$context" property, which not surprisingly indicates the context in which different form objects must be created. So, taking into account this crucial feature that corresponds to the above mentioned classes, I'm going to define the first set of form elements classes. This set is tasked with rendering "required" input and form boxes, in addition to radio buttons.

Given that, take a look at the following code listing. It shows the respective definitions for these brand new classes. They are as follows:

// define 'RequiredInputBox' class
class RequiredInputBox{
public function display(){
return '<input type="text" required="true" size="25" />';

// define 'RequiredRadioButton' class
class RequiredRadioButton{
public function display(){
return '<input type="radio" required="true" />';

// define 'RequiredCheckBox' class
class RequiredCheckBox{
public function display(){
return '<input type="checkbox" required="true" />';

As you can see, the logic implemented by the three classes shown above is extremely easy to grasp, since all that these classes do is generate the required markup for creating mandatory form elements.

Obviously, this condition is clearly demonstrated by adding to the markup in question, a "required" custom attribute, in this way indicating that all these form objects must work in a predefined "required" context.

Now that I've shown you the signatures for all the classes that render "required" web form elements, please have a look at the code for the following classes, which are responsible for displaying "normal" (non-required) radio buttons, as well as input and check boxes.

These classes look like this:

// define 'NormalInputBox' class
class NormalInputBox{
public function display(){
return '<input type="text" size="25" />';

// define 'NormalRadioButton' class
class NormalRadioButton{
public function display(){
return '<input type="radio" />';

// define 'NormalCheckBox' class
class NormalCheckBox{
public function display(){
return '<input type="checkbox" />';

In this case, the above defined classes implement the logic necessary for creating non-required web form elements. Not surprisingly, this process is nearly identical to the one used for creating required form controls, but the "required" custom attribute has been completely removed from the pertinent markup.

All right, at this stage I'm sure that you should have a pretty good idea of how all the classes that I defined a few lines before can be put to work together to demonstrate the functionality offered by the abstract factory pattern.

Do you still need additional assistance to understand the topic? Okay, don't worry, because in the last section of this article, I'm going to develop an illustrative example, where you'll see how different form objects are instantiated by the corresponding concrete factories, by using only the context to which they belong.

Want to see how this process will be performed? Go ahead and read the next few lines. I'll be there, waiting for you.

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