How can the Singleton pattern be modified to allow for multiple database connections in PHP?

The Singleton pattern restricts the instantiation of a class to a single instance, which can be problematic when dealing with multiple database connections in PHP. To modify the Singleton pattern to allow for multiple database connections, you can create a static array within the Singleton class to store the instances of the database connections based on a key or identifier.

class DatabaseConnection {
    private static $instances = [];

    public static function getInstance($connectionName) {
        if (!isset(self::$instances[$connectionName])) {
            self::$instances[$connectionName] = new self();
            // Additional logic to establish the database connection
        }
        return self::$instances[$connectionName];
    }

    private function __construct() {
        // Private constructor to prevent instantiation
    }

    // Additional database connection methods can be added here
}

// Example usage
$connection1 = DatabaseConnection::getInstance('connection1');
$connection2 = DatabaseConnection::getInstance('connection2');