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

The first step within the development process: coding form element classes - 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

A natural place to start building a form element factory is by coding the required classes to render each element. Maybe this sounds like common sense, but in fact it's a logical starting point within the development process. As you'd expect, writing the code for each form element doesn't present major difficulties due to its simplicity, but since we're dealing with many components, the task will be rather long.

A brief disclaimer is needed here: it's often a better solution to define a base class that contains the methods and properties shared by any form element, and then derive the required specific subclasses. However, in this case I won't stick to this method, simply to make the code a little more abbreviated and demonstrate the polymorphism of objects. Anyway, by the end of this series you'll have the knowledge to implement your own version of the form application.

Having said that, let's list the class source code for each form element. The first class renders a text input box, and looks like this:

// class textinput
class textinput{
  private $html;
  public function __construct($attributes=array()){
    if(count($attributes)<1){
      throw new Exception ('Invalid number of attributes');
    }
    $this->html='<input type="text" ';
    foreach($attributes as $attribute=>$value){
      $this->html.=$attribute.'="'.$value.'" ';
    }
    $this->html.='/>';
  }
  public function getHTML(){
    return $this->html;
  }
}

As I explained in the previous article, the above class generates the (X)HTML markup to display a regular input text box, which is returned by the "getHTML()" method. Let's move on to listing the class responsible for building password fields:

// class passwordinput
class passwordinput{
  private $html;
  public function __construct($attributes=array()){
    if(count($attributes)<1){
      throw new Exception ('Invalid number of attributes');
    }
    $this->html='<input type="password" ';
    foreach($attributes as $attribute=>$value){
      $this->html.=$attribute.'="'.$value.'" ';
    }
    $this->html.='/>';
  }
  public function getHTML(){
    return $this->html;
  }
}

Next, here is the list for hidden fields, file upload elements and submit images:

// class hiddeninput
class hiddeninput{
  private $html;
  public function __construct($attributes=array()){
    if(count($attributes)<1){
      throw new Exception ('Invalid number of
attributes');
    }
    $this->html='<input type="hidden" ';
    foreach($attributes as $attribute=>$value){
      $this->html.=$attribute.'="'.$value.'" ';
    }
    $this->html.='/>';
  }
  public function getHTML(){
    return $this->html;
  }
}

// class fileinput
class fileinput{
  private $html;
  public function __construct($attributes=array()){
    if(count($attributes)<1){
      throw new Exception ('Invalid number of
attributes');
    }
    $this->html='<input type="file" ';
    foreach($attributes as $attribute=>$value){
      $this->html.=$attribute.'="'.$value.'" ';
    }
    $this->html.='/>';
  }
  public function getHTML(){
    return $this->html;
  }
}

// class imageinput
class imageinput{
  private $html;
  public function __construct($attributes=array()){
    if(count($attributes)<1){
      throw new Exception ('Invalid number of attributes');
    }
    $this->html='<input type="image" ';
    foreach($attributes as $attribute=>$value){
      $this->html.=$attribute.'="'.$value.'" ';
    }
    $this->html.='/>';
  }
  public function getHTML(){
    return $this->html;
  }
}

Okay, sit tight just a little longer and jump into the next few lines. As you might guess, I'll show the remaining classes to get the form element classes completed.



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