How does encapsulating this functionality within a class or database class impact performance and maintainability?

Encapsulating functionality within a class or database class can improve performance by organizing code into reusable components and reducing redundancy. It also enhances maintainability by promoting a modular structure that is easier to update and debug.

class Database {
    private $connection;

    public function __construct($host, $username, $password, $database) {
        $this->connection = new mysqli($host, $username, $password, $database);
        if ($this->connection->connect_error) {
            die("Connection failed: " . $this->connection->connect_error);
        }
    }

    public function query($sql) {
        return $this->connection->query($sql);
    }

    public function close() {
        $this->connection->close();
    }
}

// Example usage
$db = new Database('localhost', 'username', 'password', 'database');
$result = $db->query("SELECT * FROM table");
while ($row = $result->fetch_assoc()) {
    // Process data
}
$db->close();