W założeniach ma działać analogicznie jak JS metody CreateElement() itp.
Podstawą jest klasa htmlElement - opisuje pojedynczy element drzewa będący na końcu gałęzi (liść) - to znaczy taki który nie posiada dzieci
Drugą klasą jest klasa node - opisuje ona węzeł drzewa, może posiadać dzieci którymi mogą być zarówno inne klasy node jak i htmlElement. node dziedziczy z htmlElement.
Klasom zaimplementowałem konstruktory oraz metody genHtml() służące generowaniu kodu.
Oprócz tego stworzyłem 3 dodatkowe klasy których jedynym zadaniem jest uproszcone generowanie instancji klas htmlElement i node są to:inputSubmit, textarea, inputText.
Moje pytania:
czy to jest właściwe podejście do OOP?
czy popełniłem jakieś błędy? jakie?
Konstruktory klas mają możliwość podania jednego lub dwóch argumentów. W tym celu do każdego konstruktora musiałem dodać:
<?php $this->attribs=$arg_list[1]; } ?>
Czy da się to uprościć?
Nie jestem pewien czy przypisywanie w konstruktorze $this->attribs=$arg_list[1]; (tabeli z argumentu funkcji do wnętrza klasy jest prawidłowe) Co prawda działa, ale....
Chcę zaznaczyć że są to moje pierwsze kroki w OOP i przedstawiam jedynie szkielet klasy którą mam zamiar rozbudować. Będę wdzięczny za wszelkie opinie.
Teraz kod:
<?php class htmlElement { public $tag; public $innerHtml; public $attribs; function __construct($tag) { $this->tag=$tag; $this->attribs=$arg_list[1]; } } function makeOpenTag() { $r='<'.$this->tag; return $r.'>'; } function makeCloseTag() { return '</'.$this->tag.'>'; } function genHtml(){ return $this->makeOpenTag().$this->innerHtml; } } class inputText extends htmlElement { function __construct($name) { $this->attribs=$arg_list[1]; } $this->tag='input'; $this->attribs['type']='text'; $this->attribs['name']=$name; } } class inputSubmit extends htmlElement { function __construct($name) { $this->attribs=$arg_list[1]; } $this->attribs['type']='submit'; $this->attribs['name']=$name; } } class textarea extends node { function __construct($name) { $this->attribs=$arg_list[1]; } $this->tag='input'; $this->tag='textarea'; $this->attribs['name']=$name; } } class node extends htmlElement { public $parent; public $childs; function __construct($tag){ $this->tag=$tag; $this->attribs=$arg_list[1]; } } function add($element){ $this->childs[]=$element; } function genHtml(){ $html=$this->makeOpenTag(); $html.=$this->makeCloseTag(); return $html; } } $root=new node('div'); $root->add(new textarea('ta1')); $root->add(new inputText('t3')); $n_node=new node('div'); $n_node->add(new htmlElement('input',Array( 'type'=>'button', 'name'=>'end', 'value' =>'Zakończ'))); $root->add($n_node); ?>