a little up!
after a couple days of researching and testing, here is what I ended up having:
<?php
// Error Detection system
error_reporting( E_ALL | E_STRICT ); ini_set( 'display_errors', true );
/**
* Dynamically create a form
*/
class Form
{
private $action, $method;
public function __construct($action, $method)
{
$this->action = $action;
$this->method = $method;
}
/**
* Render the dynamic form
*
* @return string
**/
public function render($inputs)
{
$output = '';
foreach ($inputs as $input) {
$output .= $input->render();
}
$output .= '';
return $output;
}
}
/**
* Parent Class for form objects
*/
abstract class FormObject
{
public $id, $class, $value;
function __construct($id=null, $value=null, $class=null)
{
// NOTE - J'ai enleve le parametre $name, puisqu'il a tout le temps la meme valeur que $id
$this->id = $id;
$this->value = $value;
$this->class = $class;
}
abstract function render();
}
/**
* Input Fields: A Type of FormObject
*/
class InputField extends FormObject
{
public $type, $checked;
// NOTE - The checked option is for the radio type inputs
public function __construct($type, $id, $value=null, $checked=null, $class=null)
{
parent::__construct($id, $value, $class);
$this->type = $type;
$this->id = $id;
$this->class = $class;
$this->value = $value;
$this->checked = $checked;
}
/**
* Generate the form element
*
* @return string
**/
public function render(){
// NOTE - Dans toutes les methodes render() j'utilise des if et pas de elseif parce qu'il s'agit de conditions independantes
$output = ' if ($this->checked != null) {
$output .= ' checked="'.$this->checked.'"';
}
if($this->value != null) {
$output .= ' value="'.$this->value.'"';
}
if ($this->class != null) {
$output .= ' class="'.$this->class.'"';
}
$output .= ' />';
return $output;
}
}
/**
* Textarea, Dropdown menus, labels
*/
class TaDdL extends FormObject
{
public $tag, $for, $size, $options;
public function __construct($tag, $id=null, $value=null, $for=null, $options=null, $size=null, $class=null)
{
parent::__construct($id, $value ,$class);
$this->tag = $tag;
$this->id = $id;
$this->for = $for;
$this->size = $size;
$this->class = $class;
$this->value = $value;
$this->options = $options;
}
/**
* Generate the form element
*
* @return string
**/
public function render(){
$output = '<'.$this->tag;
if ($this->id != null) {
$output .= ' id="'.$this->id.'"';
}
if ($this->size != null) {
$output .= ' size="'.$this->size.'"';
}
if ($this->for != null) {
$output .= ' for="'.$this->for.'"';
}
if ($this->class != null) {
$output .= ' class="'.$this->class.'"';
}
// ici je met la valeur entre les deux tags et non a l'interieur du tag d'ouverture donc je suis oblige de fermer le tag d'ouverture, d'ou le else
if ($this->value != null) {
$output .= '>'.$this->value.''.$this->tag.'>';
} elseif ($this->options != null) {
$output .= '>';
foreach ($this->options as $value => $option) {
$output .= ''.$option.'';
}
$output .= ''.$this->tag.'>';
} else {
$output .= '>'.$this->tag.'>';
}
return $output;
}
}
?>
This code works pretty well but in my opinion it doesn't have anything that makes it "superior" to a regular collection of functions, it's just re-rewritten in "the oop way".
So can anybody give me any suggestions on how I could improve that code in anyway so that it makes it significantly better than a regular collection of precedural php functions?