What are the disadvantages of using a PHP database class as a singleton?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- suppress a singleton constructor in java with powe
- Can php detect if javascript is on or not?
You can not use two database connections. You would want this because:
The disadvantages are the same as for any class that uses the Singleton pattern:
If your DB class is built to only connect to a single database, you will have problems when you have a script that needs to connect to 2 two separate databases. However, you could build the singleton class to accept multiple server configurations, and then manage them within the singleton.
Otherwise, designing a database class as a singleton is a practice that makes a lot of sense, as you can maintain tight control over how many connections a script is making at any given time.
It makes it hard to run unit tests against it and also makes it impossible to have multiple database connections. As we all know, global variables has lots of drawbacks and Singletons are no exception, only that they are a more "friendly" global variable.
I found a pretty good article about it and an old SO question as well.