PHP 封装和数据库调用约定

发布于 2024-11-03 06:10:46 字数 866 浏览 4 评论 0原文

这需要一些时间来解释。我正在创建我的第一个真实世界的 Web 应用程序,并且我会正确地完成它。我对 PHP 的经验很少,但在其他语言方面有丰富的经验,所以技术技能不是问题,更多的是语言的约定。我遵循 MVC 模式,并且正处于为应用程序实现用户注册的阶段。

为了标准化与数据库的连接,我创建了一个带有静态 getConnection 方法的 Config 类,该方法创建一个 mysqli 连接对象。这不是问题,问题是接下来的问题。

为了使我的类更具可读性,我在其中内置了各种进行数据库调用的函数。例如,我的 User 类有一个 getFriends 方法,如下所示:

class User
{
   public $id;

   public getFriends()
   {
      return UserController::getFriends($id);
   }
}

但就目前情况而言,如果我以这种方式实现它,则意味着为页面上的每个查询创建一个连接,可能在一个页面中很多次单一脚本,这太可怕了。

我正在考虑做与上面相同的事情,但是向 getFriends 传递一个 mysqli 对象,该对象又将一个对象传递给 UserController::getFriends ,但这感觉很混乱,而且坦率地说形式很差,即使它保证每个脚本只有一个连接,一个更好的改进。

我还考虑过放弃将方法完全保留在 User 内部的想法,而是直接在脚本中进行类似 UserController::getFriends($connection, $id) 的调用,并在开头声明一个 $connection 来代替 user ->getFriends()。这似乎是绝对最干净、最好的解决方案,但我不确定。

那么,本质上来说,PHP 人员通常如何做这类事情呢?

This is going to take a bit to explain. I'm creating my first real-world web application, and I'd to do it properly. I have very little PHP experience, but vast experience in other languages so technical skill isn't a problem, it's more conventions of the language. I'm following the MVC pattern, and I am at the stage where I'm implementing user registration for the application.

To standardise connections to the database, I've created a Config class with a static getConnection method, which creates a mysqli connection object. This isn't a problem, it's the next bit that is.

To make my classes a bit more readable, I have various functions built into them that make database calls. For example, my User class has a getFriends method like so:

class User
{
   public $id;

   public getFriends()
   {
      return UserController::getFriends($id);
   }
}

But as it stands now, if I implement it that way, it means creating a connection for every query on a page, probably many times in a single script, which is just horrific.

I was thinking about doing the same as above, but pass getFriends a mysqli object, which in turn passes one to UserController::getFriends as well, but that feels messy, and frankly poor form, even though it would guarantee only one connection per script, a much better improvement.

I also thought about scrapping the idea of keeping the methods inside User altogether, and instead making calls like UserController::getFriends($connection, $id) directly in the script, with a single $connection declared at the beginning, in place of user->getFriends(). That seems like the absolute cleanest, nicest solution, but I'm unsure.

So, essentially, how do PHP folks normally do this sort of thing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-11-10 06:10:46

我在我的MVC框架中所做的就是创建一个数据库连接并将其分配给Model 基类(在配置中):

$db = new database\adapters\MySQL(...);
if ( !$db->connected() ) {
  exit('Ewps!');
}

database\Model::dbObject($db);

然后,以后在任何地方,我都可以使用:

User::getFriends(13);

因为 User extends Model 并且 Model 可以访问 $db: self::dbObject()

如果我需要原始数据库连接,我可以使用 Model::dbObject()$GLOBALS['db'],但我很少需要原始连接,因为所有数据库逻辑都应该在您的模型中。

What I do in my MVC framework is create a db connection and assign it to the Model base class (in the config):

$db = new database\adapters\MySQL(...);
if ( !$db->connected() ) {
  exit('Ewps!');
}

database\Model::dbObject($db);

Then later on, anywhere, I can use:

User::getFriends(13);

because User extends Model and Model has access to $db: self::dbObject()

If I need my raw db connection, I either use Model::dbObject() or $GLOBALS['db'], but I rarely do need the raw connection, because all db logic should be in your Models.

秋意浓 2024-11-10 06:10:46

查看 单例模式 。它允许您创建一个类(数据库对象),但只允许使用其中一个类(因此不能实例化多个类)。

这意味着您只需创建一个与数据库的连接,并且该连接是共享的。

检查此处获取代码示例

Have a look into the Singleton Pattern . It allows you to create a class (a DB object), but where only one of them is ever allowed (so more than one can't be instantiated).

This means that you only have to create one connection to the DB and it's shared.

Check here for a code example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文