Can you provide examples of when using Singletons in PHP is appropriate and beneficial?

Using Singletons in PHP is appropriate and beneficial when you need to ensure that only one instance of a class exists throughout the application. This can be useful for managing resources that should be shared across different parts of the codebase, such as database connections or configuration settings.

class Database {
    private static $instance;

    private function __construct() {
        // Database connection setup
    }

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

// Usage
$db = Database::getInstance();