The Singleton and Factory Patterns in PHP: designing an object factory - Finishing the round: listing the rest of form element classes (
Page 3 of 5 )
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.