Are Singletons considered an anti-pattern in PHP development?

Singletons are considered by some developers to be an anti-pattern in PHP development because they can lead to tightly coupled code and make testing more difficult. Instead of using Singletons, a better approach is to use dependency injection to pass instances of objects where they are needed.

class Database {
    private static $instance;

    public static function getInstance() {
        if (!self::$instance) {
            self::$instance = new Database();
        }
        return self::$instance;
    }

    // Other database methods here
}

class UserRepository {
    private $db;

    public function __construct(Database $db) {
        $this->db = $db;
    }

    // Other user repository methods here
}

// Usage
$db = new Database();
$userRepository = new UserRepository($db);