I have created a form element class. The class is able to
- Create HTML form elements and
- Confirm if form has been submitted and if so, load the submitted value, for the specified form element into the object which is worked on on the page.
I have tried to make this as general as possible in order to make it reusable but since I am kind of newbie with regards to OOP it would be super cool if someone could check this and let me know if this is good OOP also if this is a good solution for what I am trying to achieve.
Here is the essential part of the class_FormControl()
class FormControl{
var $finalcontrol;
var $class = "form-control";
var $form_error;
protected function StartFormatting($name, $label){
if (isset($_POST[$name]) AND $_POST[$name] != "") {
return false;
}
$this->finalcontrol = "<label for='$name' >$label</label>";
return true;
}
public function get_control(){
return $this->finalcontrol;
}
}
class TextBox extends FormControl{
public function CreateControl($obj, $name, $label, $placeholder, $value = ""){
if($this->StartFormatting($name, $label)){
$this->finalcontrol .= "<input type='text' class='$this->class' id='$name' name='$name' placeholder='$placeholder'";
if ($value != "") {
$this->finalcontrol .= " value='$value' ";
}
$this->finalcontrol .= ">";
return true;
}
$func = "set_" . $name;
$obj->$func($_POST[$name]);
return false;
}
}
And here is how I use the class in the form page:
$r1 = New Recipe();
$tbx = new TextBox();
$ctrl1 = $tbx->CreateControl($r1, "Name", "Nombre", "Nombre", $r1->get_Name());
Now if $ctrl1 is true I go on and save the object in the database.
If $ctrl1 is false I go on and
echo $tbx->get_control();
on the right place in the page.
/Thanks!
OOP
First and foremost, you need to be thinking in terms of objects. Each object has it's own single responsibility wikipedia entry:
What you are doing is placing procedural code into class methods. This does not make it object oriented! You need to shift your mindset!
Objects
You're building a form builder. A form is comprised of elements. Instantly, I am thinking of:
Form
objectFormElement
objectThe above objects are specific representations of an Entity or ValueObject. Domain Driven Design.
FormElement
could be an Interface that all form elements (like input boxes, buttons etc) must conform to.Now all you need to do is make sure that each of your objects that implement
FormElement
return exactly what you want within theFormElement::getHtml()
method and it'll all still work great when you add new elements to theForm
, because it will be theForm
that is callinggetHtml()
in a loop on each of theFormElement
objects before adding this to it's own HTML and outputting it.Here's an example of a
TextBoxFormElement
I would use:SuperGlobals
You are using
$_POST
and$_GET
in these classes. You should not be doing this. You should be doing the following:Effectively, having any sort of
$_POST
or$_GET
couples these objects to the state of these superglobals. How are you going to test them? You're going to have to mock out (or fake) the contents of these superglobals each time you want to test them.No. What you should be doing is forgetting about your own application for a moment and coding these objects so you can use them in any application. Then, you pass in the values into the objects constructor or methods. Do not use
$_POST
or$_GET
directly within this class.Conclusion
You are not writing OO code. But I'm not going to write it for you. I will, however, write the methods that I would expect to call when using your library in the hope that you can figure out for yourself your own implementation. Keep everything separate, use a single responsibility for every object (this is not overkill, it is object oriented programming), and keep learning:
That is IT. That is the object API you should be creating. It should be as simple as that. Now go forth with this magical knowledge and learn.
Extra info: An alternative is to take a look at the Visitor Pattern. Basically, you separate the thing that generates the output from the thing that holds the data. Here's more info on the topic if you want to take a look at that.
Always remember that your code should be readable and easy to understand, and that if it helps - you should create some industry standard UML diagrams to go alongside your code.