What are the disadvantages of using a PHP database

2020-02-06 05:54发布

What are the disadvantages of using a PHP database class as a singleton?

标签: php singleton
4条回答
做个烂人
2楼-- · 2020-02-06 06:05

You can not use two database connections. You would want this because:

  • you have two databases.
  • you want to do something within a transaction when another transaction is already running on the 'current' database connection.
  • you want to use several mock database instances in your unit tests
查看更多
Explosion°爆炸
3楼-- · 2020-02-06 06:11

The disadvantages are the same as for any class that uses the Singleton pattern:

查看更多
霸刀☆藐视天下
4楼-- · 2020-02-06 06:15

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.

查看更多
我只想做你的唯一
5楼-- · 2020-02-06 06:23

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.

查看更多
登录 后发表回答