Marko_Simke Posted March 22, 2017 Report Share Posted March 22, 2017 Pozdrav iz Beca imam jedno malo kompleksnije pitanje (barem za mene :) ) , ako neko uopste shvati sta zelim. Uglavnom u pitanju je formular, programiran u php oop. Programiran tako da sam kreirao klase sa svim validacijama polja i posle polja formulara dodajem samo jednom funckijom u neogranicenom broju, to je ideja :) e sad je pitanje kod dokumenta checkbox.php gdje zelim konstruktor pozvati iz glavne klasse (formfield.class.php) i produziti ga u novu klasu (Najbolje je ako imate vremena da bacite pogled na kod jasnije ce vam biti) ali ne funkcionise, da li je neko imao iskustva sa pozivanjem konstruktora u pod konstruktoru sa promenama elemanata konstruktora u podkonstruktoru? kako to odraditi najbolje?neki tip? hvala index.php: <?php require_once 'inc/init.inc.php'; require_once 'inc/FormField.class.php'; require_once 'inc/Textarea.class.php'; require_once 'inc/Checkbox.class.php'; ?> <!DOCTYPE html> <html lang="de"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Form Renderer</title> <link rel="stylesheet" href="css/pure.min.css"> <link rel="stylesheet" href="css/layout.css"> </head> <body> <div class="wrapper"> <?php $htmlAttributes = [ 'class' => 'field-class blabla', 'title' => 'Username' ]; $field1 = new FormField('user', 'textarea', 'User', '', 'userId', 'Fehler!', $htmlAttributes); $field2 = new FormField('password', 'password', 'Passwort'); $field3 = new Textarea('nachricht', 'text', 'Nachricht'); $field4 = new Checkbox('newsletter', 'Newsletter abonnieren'); echo $field1->render(); echo '<br>'; echo $field2->render(); echo '<br>'; echo $field3->render(); echo '<br>'; echo $field4->render(); ?> </div> </body> </html> formfield.php <?php class FormField public $value; public $errorMsg; protected $type; protected $name; protected $label; protected $id; protected $htmlAttributes; public function __construct( $name, $type, $label = '', $value = '', $id = '', $errorMsg = '', $htmlAttributes = [] ) { if ($name !== '' && $name !== null) { $this->name = $name; } else { echo 'FormField Error: name not set'; exit(); } if ($type !== '' && $type !== null) { $this->type = $type; } else { echo 'FormField Error: type not set'; exit(); } if( $label === '' ) { $this->label = $name; } else { $this->label = $label; } $this->value = $value; $this->id = $id; $this->errorMsg = $errorMsg; if ( !is_array($htmlAttributes) ) { $this->htmlAttributes = []; } else { $this->htmlAttributes = $htmlAttributes; } } public function render() { return $this->renderLabel() . $this->renderField() . $this->renderError(); } public function renderLabel() { $output = '<label for="' . $this->id . '">'; $output .= $this->label . '</label>'; return $output; } public function renderField() { $output = '<input type="' . $this->type; $output .= '" name="' . $this->name . '"'; $output .= ' value="' . $this->value . '"'; $output .= $this->renderHtmlAttributes(); $output .= '>'; return $output; } public function renderError() { if ( $this->errorMsg === '' ) { return ''; } $output = '<span class="formfield-error">' . $this->errorMsg . '</span>'; return $output; } protected function renderHtmlAttributes() { if ( count($this->htmlAttributes) === 0 ) { return ''; } $attrs = ''; foreach( $this->htmlAttributes as $key => $value ) { $attrs .= ' ' . $key . '="' . $value . '"'; } return $attrs; } } checkbox.classs.php <?php class Checkbox extends FormField { public function __construct( $name, $value, $label = '', $id = '', $errorMsg = '', $htmlAttributes = [] ) { $type = 'checkbox'; FormField::__construct( $name, $type, $label, $value, $id, $errorMsg, $htmlAttributes ); } public function render() { return $this->renderField() . $this->renderLabel() . $this->renderError(); } } Link to comment Share on other sites More sharing options...
h8er Posted March 26, 2017 Report Share Posted March 26, 2017 Klasa "Checkbox" nasledjuje sve od "FormField" i nema poente prebacivati iz jednog u drugi konstraktor i ponavljati kod. Ako bas zelis ovako da uradis, mozda je bolje da ostavis praznu checkbox klasu (dodajes samo metode koje ce samo ta klasa da koristi): class Checkbox { } A u FormField-u proveri koja klasa je pozvala FormField konstruktor: if(get_class($this) == "Checkbox") { $this->type = "checkbox"; } else if ($type !== '' && $type !== null) { .... Kako ces da zapamtis kojim redosledom idu varijable u konstruktor? Sta ako pogresis redosled? mozes u formfield klasu da dodas metode, recimo: public function setName($name) { $this->name = $name; return $this; } i onda koristis: echo new Checkbox()->setName('ime')->setLabel('label')->render(); (p.s. obrati paznju na "return $this", methods chaining) ili prosledi kao array: new Checkbox(['name'=>'ime', 'label'=>'label']) Inace, gledaj sto pre da pocnes da koristis neki framework (preporuka Laravel) da ne moras da se mucis sa ovakvim stvarima. Ovo je za ucenje odlicno, ali je framework odavno postao standard u industriji. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now