Hoi,

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();

?>
Wat je doet ziet er goed uit. Ik zou zelf het iets anders hebben gedaan. Zo zou ik een input classen hebben gemaakt en dan de types laten overerven.

bijvoorbeeld


class Tekst extends Input{
    public function __construct($iName, $placeholder, $val = null){
        $this->iName = $iName;
        $this->addPlaceholder($placeholder);
        $this->addType("text");
        $this->addValue($val);
    }
}


nu kan ik een tekst veld maken zonder dat die controle die jij hebt nodig is. Nu kun je de tekst veld aanroepen met


$tekst = new Tekst('text', 'Leeftijd van persoon');
echo $tekst->getInput();


Je kunt wel of geen value meegeven, dus "$tekst = new Tekst('text', 'Leeftijd van persoon', 36);" werkt ook gewoon. Als je de constructor naar private set van input maak je het gelijk onmogelijk om die direct aan te roepen. Dus dan moet wel de $tekst etc gebruikt worden.

Nogmaals ik zie niks verkeerd van wat je doet, maar gewoon twee nieuwe OOP concepten (overerven en standaard waardes geven aan attributen in een functie) die jou formulier generator kunnen verbeteren.

Je zou zelf het volgende kunnen doen


echo $tekst = (new Tekst('text', 'Leeftijd van persoon'))->getInput();



Toevoeging op 18/10/2013 09:42:45:
Jeroen,

Bedankt voor je reactie.
Ik had niet verwacht dat het er gelijk al goed uit zou zien, maar fijn om te horen.
Jou voorbeeld neem ik zeker mee. Op dit moment gebruik ik nog geen 'extends', maar wil dit zeker wel gebruiken.

Ik had verwacht dat ik waarschijnlijk onnodig vaak 'value','placeholder',e.d. had aangemaakt. Dit zou ik kunnen voorkomen met 'extends' begrijp ik.

[edit]
Jeroen, jou laatste voorbeeld geeft een 'unexpected T_OBJECT_OPERATOR'.
[/edit]
@jeroen: ik had een vraag in combinatie met het script dat je voor mij had beoordeeld. Is dat van mij niet handiger en sneller? Het is dan enkelt

$this->newField('veld_naam', 'label_naam', 'omschrijving')

bij de manier van michael moet je alles apart toewijzen:

$input = new Input('pass');
$input->addType('password');
$input->addValue('secret');
$input->addPlaceholder('Wachtwoord');
echo $input->getInput();


Tis gewoon een vraag, ziet er namelijk verder nice uit :)
Donny Wie weet op 18/10/2013 12:17:35

@jeroen: ik had een vraag in combinatie met het script dat je voor mij had beoordeeld. Is dat van mij niet handiger en sneller? Het is dan enkelt

$this->newField('veld_naam', 'label_naam', 'omschrijving')

bij de manier van michael moet je alles apart toewijzen:

$input = new Input('pass');
$input->addType('password');
$input->addValue('secret');
$input->addPlaceholder('Wachtwoord');
echo $input->getInput();


Tis gewoon een vraag, ziet er namelijk verder nice uit :)

Wil je een stukje van dat script laten zien of heb je een link naar een topic waar het in staat?

Het kan inderdaad wel sneller met 1 regel zoals Jeroen ook in zijn voorbeeld aangeeft. Hierop heb ik het script ook aangepast.


<?php
class Input {
    public function __construct($iName, $iLabel = null,  $iPlaceholder = null, $iValue = null){
        $this->iName = $iName;
        $this->addPlaceholder($iPlaceholder);
        $this->addType('text');
        $this->addValue($iValue);
        $this->addLabel($iLabel);
    }
	
	public function addType($iType){
		$this->allowedTypes = Array('text','password','date','email','submit','checkbox','radio');
		
		if(in_array(strtolower($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 addValue($iValue){
		$this->iValue = $iValue;
	}
	
	public function addPlaceholder($iPlaceholder){
		$this->iPlaceholder = $iPlaceholder;
	}
	
	public function addLabel($iLabel){
		$this->iLabel = $iLabel;
	}
	
	public function getInput(){
		$input = '<label';
		if(isset($this->iName))
		{
			$input .= ' for="' . $this->iName . '"';
		}
		$input .= '>';
		if(isset($this->iLabel))
		{
			$input .= $this->iLabel;
		}
		$input .= '</label><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;
	}
}
?>

Hier heb ik het nou zo gedaan dat er bepaalde standaard waardes (text) en snel waardes zijn (label, placeholder, value) en je extra opties kan toevoegen indien je dit wel (type veranderen in password,date,email,etc, voor checkbox checked toevoegen)

<?php
$input = new Input('name','Naam','Naam','Jan');
echo $input->getInput();

$input = new Input('email','E-mailadres','E-mailadres','[email protected]');
$input->addType('email');
echo $input->getInput();

$input = new Input('birthday','Geboortedatum');
$input->addType('date');
echo $input->getInput();

$input = new Input('pass','Wachtwoord','Wachtwoord','secret');
$input->addType('password');
echo $input->getInput();

$input = new Input('optie','Optie 1','','check optie 1');
$input->addType('checkbox');
$input->checked(TRUE);
echo $input->getInput();
?>
Donny Wie weet op 18/10/2013 12:29:51

Ja zo heb ik het ook ongeveer gedaan. Heb alleen nog geen checkboxes en textareas en overige.

http://www.phphulp.nl/php/forum/topic/form-creator/92627/last/

Deze heb ik gemaakt

Jij bent dus ook OOP aan het leren. Jou 'form-creator' ook toegepast of alleen om te leren? Je zou jou script nog kunnen uitbreiden met verschillende types, en voor bepaalde types zoals date heb je weer andere attributen, en zo kan je er veel van leren. Ben benieuwd wat je er van maakt :-)

x
Michael - op 18/10/2013 13:19:47

[quote="Donny Wie weet op 18/10/2013 12:29:51"]
Ja zo heb ik het ook ongeveer gedaan. Heb alleen nog geen checkboxes en textareas en overige.

http://www.phphulp.nl/php/forum/topic/form-creator/92627/last/

Deze heb ik gemaakt

Jij bent dus ook OOP aan het leren. Jou 'form-creator' ook toegepast of alleen om te leren? Je zou jou script nog kunnen uitbreiden met verschillende types, en voor bepaalde types zoals date heb je weer andere attributen, en zo kan je er veel van leren. Ben benieuwd wat je er van maakt :-)


[/quote]

Voor mij is het om te leren. En als ie goed gekeurd is dan ga ik em ook gebruiken tenzij ik met een MVC werk, dan is het niet nodig
Inheritance is zééééér belangrijk voor je form elementen! Zoek die term eens op in de oop tutorial van phptuts.nl.
Hoezo is OO niet nodig wanneer je met MVC werkt? je weet dat MVC slechts een Design pattern is?

Reageren