简体   繁体   English

每个类或站点范围的数据库对象?

[英]Database object for each class or site-wide?

I have a database class which is constructed so that it opens a connection to the database when the first query is run. 我有一个数据库类,它构造为在运行第一个查询时打开与数据库的连接。 Should each of the other classes, used for the site's users, groups and so on, create one database object each? 用于站点的用户,组等的其他每个类是否应各自创建一个数据库对象? This makes the classes easy to deploy elsewhere; 这使得类易于部署到其他地方。 they will only depend on the database class. 它们将仅取决于数据库类。 This will however need me to put the credentials in each class. 但是,这需要我将凭据放入每个班级。 Another approach which seems less messy for me is create a database object and keep it in a global variable. 对我来说似乎不太麻烦的另一种方法是创建一个数据库对象并将其保存在全局变量中。 This reduces the active database connections at the same time as it makes it easier to config; 这样可以减少活动数据库的连接,同时使配置更容易; the credentials only have to be set once. 凭据只需设置一次。

Somewhere I've heard that global variables should be avoided. 我听说某处应该避免使用全局变量。 What's the best practice approach to this? 最佳做法是什么?

You could get your other classes to take a database connection object in their constructor. 您可以让其他类在其构造函数中采用数据库连接对象。 That way they don't have to know the credentials. 这样,他们就不必知道凭据。

Another common way is to use singletons for database connections. 另一种常见的方法是将单例用于数据库连接。 Essentially this is still a global variable though, but it does mean that you can control the database object instantiation in one place. 尽管从本质上讲,这仍然是一个全局变量,但是它确实意味着您可以在一处控制数据库对象实例化。

I would make a base class that only contains the connection management stuff and nothing table-specific. 我将创建一个仅包含连接管理内容而不包含表特定内容的基类。 You can then put the credentials there. 然后,您可以将凭据放在此处。

Another option is using a static variable on such a base class for storing the credentials (instead of hardcoding them in a function, which is kind of dirty). 另一种选择是在此类基类上使用静态变量来存储凭据(而不是将其硬编码到功能中,这有点脏)。

So BaseDBConnection::$username and BaseDBConnection::$password would contain the credentials, and your other DBConnection classes derive from BaseDBConnection and refer to self::$username , etc. 因此, BaseDBConnection::$usernameBaseDBConnection::$password将包含凭据,您的其他DBConnection类派生自BaseDBConnection并引用self::$username等。

You should avoid global database objects. 您应该避免使用全局数据库对象。 Your database should be opened as late as possible and closed as early as possible. 您的数据库应尽快打开,并尽早关闭。

You should use a database object internally per class and it should be only opened/closed when required. 您应该在每个类的内部使用一个数据库对象,并且仅在需要时才打开/关闭它。

I would create a 'configuration' file which would basically just store the credentials as variables and include it within the required Class files. 我将创建一个“配置”文件,该文件基本上只将凭据存储为变量并将其包含在所需的Class文件中。

This way any credential changes only require alteration of the 'configuration' file. 这样,任何凭据更改仅需要更改“配置”文件。

EDIT: 编辑:

I forgot to mention that this means a new instance of your database Class should be instantiated in every Class. 我忘了提到这意味着应该在每个类中实例化数据库类的新实例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM