OOP: Some advice please?

Hello,

I’ve started to use OOP in the real world now after learning it over the last couple of weeks and created a simple html form with css as shown here.

My html form

I believe If I nail a basic form I think I can start using more advanced things like making the form post to a database. Now I think i’ve got on OK with it, but have a few questions please:


<?php

Class FormElements {

  private $wrapper;
  
  private $form_class;
  private $form_action;
  private $form_method;
  
  private $form_legend;
  
  private $input_label;  
  private $input_type;
  private $input_name;
  
  //* 1) CONSTRUCT - the start of my CSS wrapper
  function __construct($wrapper='') {
    $this->wrapper = $wrapper;
  }
  
  //* 2) SETTER - attributes for the form
  function setAttributes($form_class,$form_action,$form_method) {
    $this->form_class = $form_class;
    $this->form_action = $form_action;
    $this->form_method = $form_method;
  }
 
  //* 3) SETTER - a title to the form in the legend 
  function setLegend($form_legend) {
    $this->form_legend = $form_legend;
  }  
  
  //* 3) SETTER - the data to create an input box
  function setInputBoxes($input_label,$input_type,$input_name) {
    $this->input_label = $input_label;
    $this->input_type = $input_type;
    $this->input_name = $input_name;
  }   
  
  //* 4) GETTER - the whole form    
  function getWholeform() {
    $output = '<div id="'.$this->wrapper.'">
	           <form class="'.$this->form_class.'" action="'.$this->form_action.'" method="'.$this->form_method.'">
               <fieldset>
                 <legend>'.$this->form_legend.'</legend>
			   <p>
                  <label for="firstname">'.$this->input_label.':</label> 
				  <input type="'.$this->input_type.'" name="'.$this->input_name.'">
               </p>';
	return $output;
  }
  
  //* 5) DESTRUCT -  with the closing tags needed for the above form  
  function __destruct() {
    $output = '</div>
	           </fieldset>
               </form>'; 
    return $output;
  }
  
}
  
//*********** Create the instance *********** //

$form = new FormElements("wrappername");

$form->setAttributes("cssform","nextpage.php","post");
$form->setLegend("This is the title of the form");
$form->setInputBoxes("Username","text","username");
echo $form->getWholeform();

?>

Questions:

  1. This look ok? Nothing silly i’ve done obvious?

  2. As you can see i’ve created a DESTRUCT method to close the wrapper on the form, but it’s never called called in the html of the page created. How can I encorporate that into my code so it’s outputted after the method getWholeform() is called?

  3. In my code I simply output ONE input box in my getWholeform() method, say I wanted three input boxes like so:



 $form->getInputBoxes("Username","text","username");
 $form->getInputBoxes("password","text","username");
 $form->getInputBoxes("secret","text","username");
 //...and keep on adding



and wanted to possibly keep on adding input boxes How could I do this?

  1. from a purely aesthetic point of view, if you view my Source Code on my page everything is all over the place. How can I tidy-up the method getWholeform() to make the html look cleaner?

  2. Is creating a form using OOP overkil and I should just use OOP to add data & request it from the database?

Many Thanks

Thanks Jaanboy,

Thats really helpful, although i’m stuggling to understand point 2, where you mention unset and how that could be use, and also how I can build array of input boxes. Then when displaying, loop through the array and display each one.

Could you maybe kindly post some examples?

Many Thanks

Chris

  1. It seems fine, apart from it’s limited re-usability, but that’s addressed in point 3.

  2. You can unset the $form object, or just add it to the end of the getWholeForm() method. Btw, the tags are the wrong way round, they should go: ‘</fieldset></form></div>’ :wink:

  3. You’d build an array of input boxes. Then when displaying, loop through the array and display each one.

  4. Just indent the html properly really.

  5. It’s not overkill no. As your code shows, you can now create a whole form with just 5 lines of code :slight_smile: