How can the use of PDO directly in a method like getDataBase() improve code structure compared to a separate class like ConnectDB?

Using PDO directly in a method like getDataBase() can improve code structure by encapsulating the database connection logic within the class that needs it, reducing the need for a separate ConnectDB class. This approach follows the principle of encapsulation, making the code more cohesive and easier to maintain.

class Database {
    private $pdo;

    public function getDataBase() {
        $this->pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
        return $this->pdo;
    }
}