Is it recommended to use classes and methods to define and output buttons in PHP, as opposed to directly echoing HTML code?

It is recommended to use classes and methods to define and output buttons in PHP instead of directly echoing HTML code for better code organization, reusability, and maintainability. By encapsulating the button functionality within a class and using methods to generate the button HTML, it becomes easier to manage and modify the button properties in one central location.

<?php
class Button {
    private $text;
    private $link;

    public function __construct($text, $link) {
        $this->text = $text;
        $this->link = $link;
    }

    public function render() {
        return '<a href="' . $this->link . '" class="btn">' . $this->text . '</a>';
    }
}

$button = new Button('Click me', 'https://example.com');
echo $button->render();
?>