Ik ben nu bezig met het leren van OOP.
Ik beheers PHP redelijk, ik zet hele applicaties in elkaar, alleen het OOP gedeelte is nieuw voor mij.
Nu heb ik een formbuilder class gebouwd, en wil hier graag meer functionaliteit in hebben.
Hieronder de Source:
<?php
class Forms {
protected $_inputs;
private $_method, $_action;
public function __construct($method,$action = NULL)
{
$this->_method = $method;
$this->_action = $action;
}
public function addInput($type,$name,$value,$placeholder = NULL,$extra = NULL) {
$this->_inputs[] = array (
'type' => $type,
'name' => $name,
'value' => $value,
'placeholder' => $placeholder,
'extra' => $extra
);
}
public function addLabel($name,$text) {
$this->_inputs[] = array (
'type' => 'label',
'name' => $name,
'text' => $text
);
}
public function addTextarea($name,$value = NULL, $placeholder = NULL) {
$this->_inputs[] = array (
'type' => 'textarea',
'name' => $name,
'value' => $value,
'placeholder' => $placeholder
);
}
public function addAtributeTo($name,$key,$value) {
$this->_atributes[] = array(
'name' => $name,
$key => $key,
$value => $value
);
}
function printInputs() {
echo '<pre>';
print_r($this->_inputs);
echo '</pre>';
}
public function printForm2() {
$html = '<form action="'.$this->_action.'" method="'.$this->_method.'">';
for ($i = 0; $i < count($this->_inputs); $i++) {
switch($this->_inputs[$i]['type']) {
case "label":
$html .= '<label name="'.$this->_inputs[$i]['name'].'">'.$this->_inputs[$i]['text'].'</label>';
break;
case "text":
case "password":
case "submit":
case "email":
$html .= '<input type="'.$this->_inputs[$i]['type'].'" ';
$html .= ' name="'.$this->_inputs[$i]['name'].'" ';
$html .= ' value="'.$this->_inputs[$i]['value'].'" ';
$html.= ' placeholder="'.$this->_inputs[$i]['placeholder'].'" ';
$html .= '> ';
$html .= ' '.$this->_inputs[$i]['extra'].'';
break;
case "textarea":
$html .= '<textarea name="'.$this->_inputs[$i]['name'].' value="'.$this->_inputs[$i]['value'].'" placeholder="'.$this->_inputs[$i]['placeholder'].'"></textarea>';
break;
}
}
$html .= '</form>';
return $html;
}
}
?>
Op deze manier gebruik ik de class:
<?php
$form3 = new Forms('post');
$form3->addLabel('username','Username : ');
$form3->addInput('text','username','','Username','<br>');
$form3->addLabel('password','Password : ');
$form3->addInput('password','password','','Password','<br>');
$form3->addInput('submit','submit_login','Login2');
print($form3->printForm2());
?>
Is dit een goede opbouw?
En hoe verwerk ik het addAtributeTo gedeelte in deze class?
Ik was al zover om als argumenten: $name,$key,$value te accepteren.
Maar hoe verwerk ik dat in deze class?
Alle tips zijn welkom. Gelieve geen code te geven, maar meer een duw in de goede richting ^^