Ik probeer de overgang van PHP Procedural naar PHP OOP te maken.
Om te begrijpen hoe OOP werkt en op welke manier het nuttiger kan zijn dan procedural wil ik graag jullie mening, opmerkingen, kritiek, etc horen over mijn eerste OOP script.
<?php
function printr($a){
echo '<pre>';print_r($a);echo'</pre>';
}
class Form {
public function __construct($fName){
$this->fName = '<form method="POST" name="' . $fName . '">'.PHP_EOL;
}
public function getForm(){
return $this->fName;
}
public function endForm(){
return '</form>'.PHP_EOL;
}
}
class Input {
public function __construct($iName){
$this->iName = $iName;
}
public function addType($iType){
$this->allowedTypes = Array('text','password','date','email','submit','checkbox','radio');
if(in_array($iType,$this->allowedTypes))
{
$this->iType = $iType;
}
else
{
throw new Exception('Input Type not allowed');
}
}
public function checked($iChecked){
if(($this->iType == 'checkbox' || $this->iType == 'radio') && $iChecked == TRUE)
{
$this->iChecked = TRUE;
}
else
{
$this->iChecked = FALSE;
}
}
public function group($iGroup){
if($iGroup == TRUE)
{
$this->iGroup = TRUE;
}
}
public function addValue($iValue){
$this->iValue = $iValue;
}
public function addPlaceholder($iPlaceholder){
$this->iPlaceholder = $iPlaceholder;
}
public function getInput(){
$input = '<input';
if(isset($this->iType))
{
$input .= ' type="' . $this->iType . '"';
}
if(isset($this->iName))
{
if(isset($this->iType) && ($this->iType == 'checkbox' || $this->iType == 'radio'))
{
$input .= ' name="' . $this->iName. '[]"';
}
else
{
$input .= ' name="' . $this->iName . '"';
}
$input .= ' id="' . $this->iName . '"';
}
if(isset($this->iValue))
{
$input .= ' value="' . $this->iValue . '"';
}
if(isset($this->iPlaceholder))
{
$input .= ' placeholder="' . $this->iPlaceholder .'"';
}
if(isset($this->iChecked) && $this->iChecked == TRUE)
{
$input .= ' checked="checked"';
}
$input .= ' /><br />'.PHP_EOL;
return $input;
}
}
class Select {
public function __construct($sName){
$this->sName = $sName;
}
public function addOption($sOptionValue,$sOption){
$this->sOption[][$sOptionValue] = $sOption;
}
public function getSelect(){
$selectbox = '<select name="' . $this->sName . '">'.PHP_EOL;
foreach($this->sOption AS $optionArray)
{
foreach($optionArray AS $value=>$option)
{
$selectbox .= '<option value="' . $value . '">' . $option . '</option>'.PHP_EOL;
}
}
$selectbox .= '</select><br />'.PHP_EOL;
return $selectbox;
}
}
class Box {
public function __construct($bName){
$this->bName = $bName;
}
public function addValue($bValue){
$this->bValue = $bValue;
}
public function addPlaceholder($bPlaceholder){
$this->bPlaceholder = $bPlaceholder;
}
public function getBox(){
$box = '<textarea name="' . $this->bName . '"';
if(isset($this->bPlaceholder)) { $box .= ' placeholder="' . $this->bPlaceholder . '"'; }
$box .= '>';
if(isset($this->bValue)) { $box .= $this->bValue; }
$box .= '</textarea><br />'.PHP_EOL;
return $box;
}
}
class Button {
public function __construct($bValue){
$this->bValue = $bValue;
}
public function getButton(){
return '<button>' . $this->bValue . '</button><br />'.PHP_EOL;
}
}
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post')
{
printr($_POST);
}
$form = new Form('test');
echo $form->getForm();
$select = new Select('aanhef');
$select->addOption('','Maak een keuze');
$select->addOption('Meneer','Mr.');
$select->addOption('De heer','Dhr.');
$select->addOption('Mevrouw','Mw.');
$select->addOption('Dokter','Dr.');
$select->addOption('Anders','Anders');
echo $select->getSelect();
$input = new Input('name');
$input->addType('text');
$input->addValue('Jan');
$input->addPlaceholder('Naam van persoon');
echo $input->getInput();
$input = new Input('age');
$input->addType('text');
$input->addValue('36');
$input->addPlaceholder('Leeftijd van persoon');
echo $input->getInput();
$input = new Input('pass');
$input->addType('password');
$input->addValue('secret');
$input->addPlaceholder('Wachtwoord');
echo $input->getInput();
$input = new Input('optie');
$input->addType('checkbox');
$input->addValue('check optie 1');
$input->checked(TRUE);
echo $input->getInput();
$input = new Input('optie');
$input->addType('checkbox');
$input->addValue('check optie 2');
$input->checked(FALSE);
echo $input->getInput();
$input = new Input('optie');
$input->addType('radio');
$input->addValue('radio optie 1');
$input->checked(TRUE);
echo $input->getInput();
$input = new Input('optie');
$input->addType('radio');
$input->addValue('radio optie 2');
$input->checked(FALSE);
echo $input->getInput();
$box = new Box('message');
$box->addValue('');
$box->addPlaceholder('Typ hier je bericht');
echo $box->getBox();
$button = new Button('Opslaan');
echo $button->getButton();
echo $form->endForm();
?>