I am in the early stages of building a series of classes which will construct various types of form-input controls in PHP. Here is sample of the beginning of my code, which will later construct a text input control:
<?php
class TextInput {
//These properties are reqiured, and are defined on an as-needed basis
public $name;
//These properties are optional, but can be defined on an as-needed basis
public $disabled = false;
public $maxLength = false;
public $readOnly = false;
public $size = false;
public $type = "text";
//New to HTML5
public $autoComplete = "off";
public $autoFocus = false;
public $listSuggestionArray = false;
public $max = false;
public $min = false;
public $multiple = false; //Works for "email" type only
public $pattern = false;
public $placeHolder = false;
public $required = false;
public function __construct() {
//Nothing to do!
}
//Build the text input control
public function build() {
}
}
?>
As you can see, this class is already quite large, even though it doesn't do anything! My main question is, should I continue to build these classes to handle textareas, file inputs, etc..., considering that it would obviously put more of a load on the server to generate these?
As far as I can tell here are the pros and cons:
Pros:
Con:
Taking these into consideration, should I use this route, or revert to writing them out manually in HTML?
Thank you for your time.
I think that this could be a good approach if you're wanting to also tie your input objects in with something else. Like say, a validation/message response where your validation object passes data off to your input helper. This should allow an 'easy' way to provide some user functionality in your forms.
If you aren't looking to incorporate some other functionality into this class at some point and you just want to spit out some HTML, and are worried about performance, you should just type it out raw.
A suggestion
With your current strategy you're gonna wind up copying a lot of things over and over. When you want to create a password
input or a textarea
you'll wind up copying a lot of these same variables!
What you could do as an alternative is create a class that your form generator objects can extend from that provide some general functionality/properties for the appropriate types of HTML elements. Obviously I couldn't even begin to tell you how to incorporate this into your own strategy without knowing a bigger picture of what you're trying to do and how you're trying to do it.