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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: