How do you create multiple DB connections using Singleton pattern? Or maybe there's better approach, to share the same class but multiple connections?
How about using a Factory pattern to return the same instance for each connection, e.g.
ConnectionFactory::getInstance(ConnectionFactory::DEVELOPMENT);
Returns a Connection instance for a connection to the development database.
Instantiation of the Connection should only be performed by the ConnectionFactory, which can keep references to those instances in a static array, keyed by the connection type. This avoid the singleton pattern, but ensures you only maintain a single instance of each Connection.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With