Home arrow PHP arrow Page 5 - The Singleton and Factory Patterns in PHP: designing an object factory

Instantiating form element objects: a simple use of the form element factory - PHP

This article, the second part of a series, is focused on implementing the Factor Pattern by designing a form element factory.

TABLE OF CONTENTS:
  1. The Singleton and Factory Patterns in PHP: designing an object factory
  2. The first step within the development process: coding form element classes
  3. Finishing the round: listing the rest of form element classes
  4. The Factory Pattern in action: developing the "formElementFactory" class
  5. Instantiating form element objects: a simple use of the form element factory
By: Alejandro Gervasio
Rating: starstarstarstarstar / 14
November 09, 2005

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Creating form element objects through the form element factory is a fairly straightforward process. Object instantiation is handled internally behind the interface of the class, so the code looks compact and readable. Let's start factoring some form element objects, to clarify the usage of the factory:

$textinput=formElementFactory::createElement
('textinput',array
('name'=>'fname','maxlength'=>'20'));

$passwordinput=formElementFactory::createElement
('passwordinput',array
('name'=>'password','maxlength'=>'20'));

$hiddeninput=formElementFactory::createElement
('hiddeninput',array
('name'=>'hiddendata','value'=>'default'));

$imageinput=formElementFactory::createElement
('imageinput',array
('name'=>'imgdata','src'=>'subimage.gif'));

$fileinput=formElementFactory::createElement
('fileinput',array('name'=>'upload','value'=>'1'));

The above snippet simply creates several form objects. Note that object instantiation is performed behind the scenes, each time the "createElement()" method is invoked with the proper parameters. Since this method is static, it's possible to call it outside the class context by using the (::) resolution scope operator.

If you think that the above examples are not complete, here is code to instantiate the rest of form element objects:

$radiobutton=formElementFactory::createElement
('radiobutton',array
('name'=>'radio1','value'=>'1','checked'=>'true'));

$textarea=formElementFactory::createElement
('textarea',array('name'=>'comments','value'=>'fill
this field'));

$checkbox=formElementFactory::createElement
('checkbox',array
('name'=>'checkbox1','value'=>'1','checked'=>'true'));

$button=formElementFactory::createElement
('button',array('name'=>'button1','value'=>'button'));

$submitbutton=formElementFactory::createElement
('submitbutton',array
('name'=>'send','value'=>'Send'));

$resetbutton=formElementFactory::createElement
('resetbutton',array
('name'=>'reset','value'=>'Reset'));

$selectbox=formElementFactory::createElement
('selectbox',array
('name'=>'select','size'=>'1','options'=>array
('1'=>'value1','2'=>'value2')));

Finally, let's implement a basic loop, for roughly rendering some objects:

// make array with form element objects
$formElements=array($textinput,$passwordinput,$hiddeninput,$radiobutton,
 $textarea,$checkbox,$submitbutton);
// display form elements
foreach($formElements as $element){
  echo $element->getHTML().'<br />';
}

After executing the above snippet, what you get is a crude visual presentation of the form elements included in the array. Of course, a more polished look is highly desired, but for the moment, let's settle for the elements' rendering process of this example.

As you can see, the defined "formElementFactory" class is doing its job quite well, despite its simplicity. Definitely, we've stepped forward to use the Factory Pattern within the form creation process, while the overall code has been maintained at a simple level.

Notice the evident separation between object instantiation and the rest of client code. There are no "new" constructs that indicate where the objects are created. The whole process is handled by the internals of the "formElementFactory" class.

There are still many aspects that need to be improved within the application, but you should remember that this is merely the beginning. The logic of the factory class must be modified, in order to get a higher level of abstraction to instantiate form objects, as well as get more control over visual presentation.

For many of these features to work, you'll have to wait for the next part of the series. However, these are just a few ideas to get you started using the Factory pattern in real applications.

Wrapping up

Through this article, I've exposed the basics for implementing the Factory pattern in a real situation: building object-oriented forms. To fit this objective, a form element factory class has been defined, by showing that object instantiation can be completely decoupled from the client code, so a greater level of abstraction for rendering web-based forms is obtained within a program.

Over the next part of the series, I'll add some features to the factory class, in order to encapsulate element rendering code within its structure. Meanwhile, play with the code and study its functionality. See you in the next part!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

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