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

Finishing the round: listing the rest of 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

Now that you have the code for input text boxes, hidden fields, file input fields and so forth, I'll finish writing the rest of the form element classes, that is, radio buttons, checkboxes, select boxes and text area fields, to name a few.

Notice that most of element classes are similar in their source code. This might lead you to think that all of the classes can be unified into one generic class that would accept one parameter (the element's name), and accordingly return the corresponding (X)HTML code.

While this method might be valid for reducing the length of code, it's preferable to keep each form element class as a separate entity within the application, allowing you to maintain a greater level of flexibility.

Now, it's time to finish the coding round by listing the classes for the rest of the form elements. Here is the code for radio buttons, checkboxes, and buttons:

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

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

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

Finally, below are the classes for rendering submit buttons, reset buttons, text areas and select boxes:

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

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

// class textarea
class textarea{
  private $html;
  public function __construct($attributes=array()){
    if(count($attributes)<1){
      throw new Exception ('Invalid number of
attributes');
    }
    $this->html='<textarea ';
    $textvalue='';
    foreach($attributes as $attribute=>$value){
      ($attribute!='value')?$this->html.=$attribute.'="'.$value.'"':$textvalue=$value;
    }
    $this->html=preg_replace("/\"? $/","\">",$this-
>html);
    $this->html.=$textvalue.'</textarea>';
  }
  public function getHTML(){
    return $this->html;
  }
}

// class selectbox
class selectbox{
  private $html;
  public function __construct($attributes=array()){
    if(count($attributes)<1){
      throw new Exception ('Invalid number of
attributes');
    }
    $this->html='<select ';
    $options='';
    foreach($attributes as $attribute=>$value){
      if($attribute!='options'){
        $this->html.=$attribute.'="'.$value.'" ';
      }
      else{
        foreach($value as $values=>$label){
          $options.='<option value="'.$values.'">'.$label.'</option>';
        }
      }
    }
    $this->html=preg_replace("/\"? $/","\">",$this-
>html);
    $this->html.=$options.'</select>'; 
  }
  public function getHTML(){
    return $this->html;
  }
}

By this point, most of the hard work has been done. Now that you have all of the form element classes defined, I can go one step further and start writing the form element factory. Thus, click below and keep reading.



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