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