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

?>
@Erwin, ik snap je redenen en ik weet dat public ook direct aanpasbaar is.
Ik was van plan om __toString() toe te passen, zoals ik nu heb, maar ik wou gewoon even weten wat voor commentaar erop komt:

<?php
error_reporting(E_ALL);
class Form {
	private $_form = array();
	private $_method;
	private $_methodes = array('POST','GET');
	private $_action;
	private $_enctype;
	private $_enctypes = array('application/x-www-form-urlencoded','multipart/form-data','text/plain','upload');
	private $_error = array();
	
	public function __construct($action,$method='POST',$enctype='application/x-www-form-urlencoded'){
		$this->_action = $action;
		$this->_method = strtoupper($method);
		$this->_enctype = strtolower($enctype);
		if(strtolower($this->_method) == 'upload') {
			$this->_method = 'multipart/form-data';
		}

		
		if(!in_array($this->_method,$this->_methodes)) {
			$this->_error[] = 'Deze methode is niet toegestaan! Toegestane methodes: ' . implode(', ',$this->_methodes);
		}
		if(!in_array($enctype,$this->_enctypes)) {
			$this->_error[] = 'Deze enctype is niet toegestaan! Toegestane enctypes: ' . implode(', ',$this->_enctypes);
		}
		if(empty($this->_error)) {
			$this->_form[]='<form method="' . $this->_method . '" action="' . $this->_action . '" ecntype="' . $this->_enctype . '">';
		} else {
			throw new Exception('De form kon niet gegenereerd worden! | ' . implode(' | ',$this->_error));
		}
	}
	
	public function append($input) {
		$this->_form[]= $input;
	}
	
	public function __toString() {
		return implode("\n\r",$this->_form) . '</form>';
	}
}


class Input {
	private $_type;
	private $_types = array('hidden','text','password','radio','checkbox','select','file','textarea','button','submit','reset');
	private $_input;
	
	public function __construct($type, $name,$value='',$description='',$newLine=false) {
		$differs = array('textarea','select');
		if(in_array(strtolower($type),$this->_types)) {
			if($description<>'') {
				$this->_input = '<label for="' . $name . '">' . $description . '</label>';
			}
			$this->_type = strtolower($type);
			if($this->_type == 'textarea') {
				$this->_input .= '<textarea name="' . $name . '" id="' . $name . '">' . $value . '</textarea>';
			} elseif($this->_type == 'select') {
				if(is_array($value)) {
					$this->_input .= '<select name="' . $name . '" id="' . $name . '">';
					foreach($value as $value => $description) {
						$this->_input .= '<option value="' . $value . '" id="' . $name . '">' . $description . '</option>';
					}
					$this->_input .= '</select>';
				} else {
						throw new Exception('De select input heeft meerdere keuze opties! Defineer deze als array("Waarde"=>"Omschrijving")');
				}
			} else {
				$this->_input .= '<input type="' . $type . '" name="' . $name . '" id="' . $name . '"value="' . $value . '">';
			}
			if($newLine) {
				$this->_input .= '<br>';
			}
		} else {
			throw new Exception('Deze type input is niet toegestaan!');
		}
	}
	public function __toString() {
		return $this->_input;
	}
}

$myForm = new Form('login.php');
$myForm->append(new Input('text','login_user','Gebruikersnaam','Uw gebruikersnaam:',true));
$myForm->append(new Input('password','login_pass','password','Uw wachtwoord:',true));
$myForm->append(new Input('submit','login_btn','Login!'));
echo $myForm;


?>


En over die attributen heb je ergens wel gelijk, en eigenlijk wil ik deze dus zelf als 'geavanceerde' optie toevoegen bij de new Input()

Reageren