Should I create a class for each "input", or just methods of a class "form" enough?

3

Well, I have a logical doubt. I started learning PHP OO recently, so this question is more for educational purposes than anything else.

My goal is to create a class "form" that will build HTML forms. This class will have attributes like "id", "name", "method" and "action" and methods like openForm() - to start the form with such attributes, closeForm() - to close the form , and the patterns like set and get for attributes.

My initial idea was to create methods for each input of this form in this same class, such as newInputText() , which would create an input of type text, newButtonSubmit() , which would create a submit button and so on (note that each of these methods would have its own HTML parameters like "id", "name", "class", and so on).

My question is: the creation of inputs and buttons , for example, should be done using class Or should I create a new separate class for each of them, with their own attributes and methods? What would be the most "optimized" way for such an application?

Hypothetical code example:

$form1 = new Form("teste", "teste", "#");

$form1->openForm();

$form1->newInput("input1", "input1", "Oi");
$form1->newButtonSubmit("btnSend", "btnSend", "Enviar");

$form1->closeForm();
    
asked by anonymous 22.03.2017 / 14:14

1 answer

0

Try to work with the use of Namespaces and the Principle of Single Responsibility (where a class must have only ONE responsibility). This will give you more code reuse and less painful maintenance.

    
28.03.2017 / 20:03