Jump to content

AzizLight

New Members
  • Posts

    4
  • Joined

  • Last visited

AzizLight's Achievements

Newbie

Newbie (1/14)

0

Reputation

  1. a little up! after a couple days of researching and testing, here is what I ended up having: <?php // Error Detection system error_reporting( E_ALL | E_STRICT ); ini_set( 'display_errors', true ); /** * Dynamically create a form */ class Form { private $action, $method; public function __construct($action, $method) { $this->action = $action; $this->method = $method; } /** * Render the dynamic form * * @return string **/ public function render($inputs) { $output = ''; foreach ($inputs as $input) { $output .= $input->render(); } $output .= ''; return $output; } } /** * Parent Class for form objects */ abstract class FormObject { public $id, $class, $value; function __construct($id=null, $value=null, $class=null) { // NOTE - J'ai enleve le parametre $name, puisqu'il a tout le temps la meme valeur que $id $this->id = $id; $this->value = $value; $this->class = $class; } abstract function render(); } /** * Input Fields: A Type of FormObject */ class InputField extends FormObject { public $type, $checked; // NOTE - The checked option is for the radio type inputs public function __construct($type, $id, $value=null, $checked=null, $class=null) { parent::__construct($id, $value, $class); $this->type = $type; $this->id = $id; $this->class = $class; $this->value = $value; $this->checked = $checked; } /** * Generate the form element * * @return string **/ public function render(){ // NOTE - Dans toutes les methodes render() j'utilise des if et pas de elseif parce qu'il s'agit de conditions independantes $output = ' if ($this->checked != null) { $output .= ' checked="'.$this->checked.'"'; } if($this->value != null) { $output .= ' value="'.$this->value.'"'; } if ($this->class != null) { $output .= ' class="'.$this->class.'"'; } $output .= ' />'; return $output; } } /** * Textarea, Dropdown menus, labels */ class TaDdL extends FormObject { public $tag, $for, $size, $options; public function __construct($tag, $id=null, $value=null, $for=null, $options=null, $size=null, $class=null) { parent::__construct($id, $value ,$class); $this->tag = $tag; $this->id = $id; $this->for = $for; $this->size = $size; $this->class = $class; $this->value = $value; $this->options = $options; } /** * Generate the form element * * @return string **/ public function render(){ $output = '<'.$this->tag; if ($this->id != null) { $output .= ' id="'.$this->id.'"'; } if ($this->size != null) { $output .= ' size="'.$this->size.'"'; } if ($this->for != null) { $output .= ' for="'.$this->for.'"'; } if ($this->class != null) { $output .= ' class="'.$this->class.'"'; } // ici je met la valeur entre les deux tags et non a l'interieur du tag d'ouverture donc je suis oblige de fermer le tag d'ouverture, d'ou le else if ($this->value != null) { $output .= '>'.$this->value.''.$this->tag.'>'; } elseif ($this->options != null) { $output .= '>'; foreach ($this->options as $value => $option) { $output .= ''.$option.''; } $output .= ''.$this->tag.'>'; } else { $output .= '>'.$this->tag.'>'; } return $output; } } ?> This code works pretty well but in my opinion it doesn't have anything that makes it "superior" to a regular collection of functions, it's just re-rewritten in "the oop way". So can anybody give me any suggestions on how I could improve that code in anyway so that it makes it significantly better than a regular collection of precedural php functions?
  2. Thanks for your answer. I forgot the keyword 'extends' indeed, that was a mistake of me. But what I wasn't sure was the synthax of the constructor of the child class. I wasn't sure if I had to repeat the parameters or not. The answer is: yes and no. It depends what I actually want. If I want the Bar class to have two parameters then I have to include all the parameters. So the correct synthax would be the second one. For the first question, the "logical" synthax I provided is the correct one too. In the case of a class with a constructor that has 30 parameters, I figured that either the class is very poorly writter or the level of the code is so high that by the time I write a class with a constructor that takes 30 parameters I'll know how to manage it So after asking questions in the Freenode ##php irc channel and researching and testing (and...that's it ) I managed to answer my own questions Anyway, thanks again for you answer
  3. Hi everybody, There are some stuff about OOP that are not clear for me: 1- when I declare a class, and create a constructor with several optional parameters as such: class FooBar{ public $foo, $bar, $foobar; public function __construct($foo, $bar=null, $foobar=null){ ... } } If I then want to create an instance of that object with $foo and $foobar as parameters ($foo being madatory, $foobar being one of the optional parameters), how would I proceed? The only way that I see is creating it as such: $foobar1 = new FooBar(1, null, 3); but if I have 30 optional parameters and I want to create an instance of the object with only one of the optional parameters I will have to type null 29 times and put the value of the wanted optional parameter at the right place? Not convenient, so how should I proceed? 2- When I create a class Foo: class Foo{ public $foo; public function __construct($foo){ ... } } and a another class bar that extends foo: class Bar{ public $bar; public function __construct($bar){ ... } } Is this the correct synthax for the Bar class? Or should it be something like that: class Bar{ public $bar; public function __construct($foo, $bar){ ... } }
  4. Hi everybody, I followed the OOP PHP course on this site which was very helpful. I'm now working in a small company that uses a set of functions to dynamically create forms. So for instance they have a function called beginForm that takes two arguments (the form action and the legend tag content - the title of the form) and that creates the form and fieldset opening tags and the legend tag. And this way the programmer creates forms my calling a set of functions. Now my job is to improve that system by porting it to OOP. Now since this is my first OOP PHP project (and for that matter my first PHP project too) I need some advice on how to handle this. Should I just create one class instead of each function and extend a base "input" class to create several types of input fields or should I create one big class (in which case I have no idea how to proceed), or should I proceed in another way? The idea is to be able to easily create forms dynamically so that we dont have to retype the code everytime we want to add a form (and ultimately let a non-programmer create forms using a form - just a random idea...). What do you thnk?
×
×
  • Create New...