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

Understanding how the abstract factory pattern works - 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



In order to understand more easily how diverse web form objects can be instantiated in accordance with a predefined context, below I set up an educational example. It shows in a step-by-step format how to display a few "required" and "normal" form elements, in consonance with a given instance of a factory class.

The source code that corresponds to the example in question is as follows:

// instantiate 'RequiredFormElementFactory' object
   // (works in the 'required' context)
$factoryInstance=new RequiredFormElementFactory();
// displays required form elements
echo '<form>';
echo $reqInputBox->display();
echo $reqRadioButton->display();
echo $reqCheckBox->display();
echo '</form>';
// instantiate 'NormalFormElementFactory' object
   // (works in the 'normal' context)
$factoryInstance=new NormalFormElementFactory();
// displays normal form elements
echo '<form>';
echo $inputBox->display();
echo $radioButton->display();
echo $checkBox->display();
echo '</form>';
catch(Exception $e){
echo $e->getMessage();

Certainly, you must agree with me that the previous example is indeed demonstrative with reference to showing the functionality of the abstract factory pattern! As you can see, the above script first creates an instance of the "RequiredFormElementFactory" class, and then displays a simple web form, which contains a few "required " form elements. Logically, this is because this concrete factory makes sure that all the instantiated objects belongs to the "required" context.

Next, the example continues doing its thing and creates a new instance of the "NormalFormElementFactory" class, which not surprisingly is tasked with spawning form objects that belong to the "normal" context. Pretty good, right?

As you hopefully learned, in all cases, the concrete factories are completely responsible for making sure that the correct types of objects are returned to client code.

As with all of my articles on web development with PHP, I suggest that you experiment with using and modifying all the classes that I developed here, so you can acquire a better background on how this design pattern works.

Final thoughts

That's all for the moment. In this three-part series, I provided you with a neat set of illustrative examples on how to build and use abstract factory classes with PHP 5.

Although it's more probable that you'll use only singletons and concrete factories during the development of object-oriented applications, there will be situations where you'll need to use abstract factory classes to create objects that must exist only in a predefined context.

See you in the next PHP development tutorial!

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