HomePHP 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.
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.