What are some best practices for creating a class for generating HTML forms in PHP?

When creating a class for generating HTML forms in PHP, it's important to follow best practices to ensure the code is clean, maintainable, and reusable. Some best practices include using object-oriented principles, separating logic from presentation, and providing flexibility for customization.

<?php
class FormGenerator {
    private $fields = [];

    public function addField($name, $type, $label) {
        $this->fields[] = ['name' => $name, 'type' => $type, 'label' => $label];
    }

    public function generateForm() {
        $form = '<form>';
        foreach ($this->fields as $field) {
            $form .= '<label>' . $field['label'] . '</label>';
            $form .= '<input type="' . $field['type'] . '" name="' . $field['name'] . '"><br>';
        }
        $form .= '<input type="submit" value="Submit">';
        $form .= '</form>';
        return $form;
    }
}

// Example usage
$form = new FormGenerator();
$form->addField('username', 'text', 'Username');
$form->addField('password', 'password', 'Password');
echo $form->generateForm();
?>