In PHP, what are the recommended approaches for handling database connections and objects in larger, more complex projects compared to smaller, simpler scripts?

In larger, more complex projects, it is recommended to use a design pattern such as Dependency Injection or a database abstraction layer to handle database connections and objects. This helps in managing dependencies, improving testability, and making the codebase more maintainable.

// Example using Dependency Injection to handle database connections

class Database {
    private $connection;

    public function __construct(PDO $connection) {
        $this->connection = $connection;
    }

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

    // Other database methods can be added here
}

// Usage example
$dsn = 'mysql:host=localhost;dbname=testdb';
$username = 'username';
$password = 'password';
$pdo = new PDO($dsn, $username, $password);

$database = new Database($pdo);
$result = $database->query('SELECT * FROM users');