php共享数据库连接(设计模式帮助)

发布于 2024-10-18 22:26:37 字数 748 浏览 3 评论 0原文

我有一个小型 php 应用程序,我想在其上构建一个数据库抽象层,并使用一些“模型”类型类。

我正在使用 ezSQL_mysql 来完成数据库工作。

我的问题是设计应用程序的最佳方法是什么?我应该使用单例模式来共享数据库连接吗?我的“模型”类应该扩展 ezSQL_mysql 吗?或者也许我完全偏离了基地,需要做点别的事情。

我需要的是这样的

Controller.php

   $db = new ezSQL_mysql($db_user, $db_passwd, $db_database, $db_host);


   $user = new User();
   $user->update_email($new_email);

   $sale = new Sale();
   $sale->purchase($amount); 

User_model.php

class User {

   /* uses $db connection */
   function update_email(){
     /* do something */
   };
}

Sale_model.php

class Sale {   
   /* uses $db connection*/

   function purchase () {
    /* do something */ 
   }
}

I have a small php app that I want to build a layer of db abstraction on top of with a few "model" type classes.

I'm using ezSQL_mysql to do the db work.

My question is what is the best way to design the app? Should I use a singleton pattern to share the db connection? Should my "model" classes extend ezSQL_mysql ? Or maybe I'm totally off base here and need to do something else.

What I need is something like this

Controller.php

   $db = new ezSQL_mysql($db_user, $db_passwd, $db_database, $db_host);


   $user = new User();
   $user->update_email($new_email);

   $sale = new Sale();
   $sale->purchase($amount); 

User_model.php

class User {

   /* uses $db connection */
   function update_email(){
     /* do something */
   };
}

Sale_model.php

class Sale {   
   /* uses $db connection*/

   function purchase () {
    /* do something */ 
   }
}

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

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

发布评论

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

评论(3

佞臣 2024-10-25 22:26:37

数据库连接单例可能会出现问题。幸运的是,连接共享并不是必需的。您可以拥有一个连接管理器类,当给定以前使用过的主机、用户和数据库名称时,它会返回已打开的连接。

abstract class ConnectionManager {
    protected $connections = array();
    function connect($host, $db, $user, $pw) {
        if (! isset($this->connections[$host][$db][$user])) {
            $this->connections[$host][$db][$user] = $this->newConnection($host, $db, $user, $pw));
        }
    }
    abstract protected function newConnection($host, $db, $user, $pw);
}

class EzSQLConnectionManager extends ConnectionManager {
    protected function newConnection($host, $db, $user, $pw) {
        return new ezSQL_mysql($user, $passwd, $database, $host);
    }
}

这里没有讨论如何安全地存储用户凭据。密码不应该随意地在整个脚本中传播,这种方法可能会导致您这样做。

A DB connection singleton could be problematic. Fortunately, it's not necessary for connection sharing. You can have a connection manager class that, when given a previously used host, user and database name, returns the already open connection.

abstract class ConnectionManager {
    protected $connections = array();
    function connect($host, $db, $user, $pw) {
        if (! isset($this->connections[$host][$db][$user])) {
            $this->connections[$host][$db][$user] = $this->newConnection($host, $db, $user, $pw));
        }
    }
    abstract protected function newConnection($host, $db, $user, $pw);
}

class EzSQLConnectionManager extends ConnectionManager {
    protected function newConnection($host, $db, $user, $pw) {
        return new ezSQL_mysql($user, $passwd, $database, $host);
    }
}

Not addressed here is how to securely store user credentials. Passwords shouldn't be spread willy-nilly throughout scripts, which this approach might lead you to do.

深者入戏 2024-10-25 22:26:37

对于小型应用程序,一定要使用域对象模式。也就是说,每个对象都代表数据库实体。

然后您可以决定是否使用映射器模式向数据库添加一些更复杂的查找。

如果您更喜欢经过测试的解决方案,可以检查 Zend_Db。它涵盖了您需要的一切,从数据库适配器、工厂等。它基本上实现了网关模式,但参考手册中已经显示了诸如映射器之类的实现。

其他解决方案包括 ORM,例如 Doctrine 或 Propel。

模式不应该被过度使用。如果它是一个小应用程序,请尽可能简单地解决问题。如果您将此作为实验,请尝试使用一些 Doctrine。这些库有时会非常令人惊奇。

Well for small application, definitely use domain object pattern. That is, every object represents db entity.

Then you can decide weather you'll use mapper pattern to add some more complex lookups to db or not.

If you prefer tested solutions, you can check Zend_Db. It covers everything you need, from db adapters, factory, etc. It basically implements gateway pattern, but implementing something like mapper is already shown in reference manual.

Other solutons would include ORM like Doctrine or Propel.

Patterns should not be overused. If it's a small app, solve the problem as easy as possible. If you're doing this as an experiment, try to play with something Doctrine. Those libraries can sometimes be quite amazing.

陌上青苔 2024-10-25 22:26:37

好吧......所以这个小应用程序的需求像往常一样增长,我决定使用 MVC 框架,而不是滚动我自己的松散类集来管理持久连接并抽象数据库层。

所以现在我使用 CodeIgniter http://www.codeigniter.com/ 它基本上完成了我想要的以更易于管理的方式进行。

感谢其他答案。

Okay... so the small app's requirements grew, as per usual and I decided to go with an MVC framework, rather than rolling my own loose set of classes to manage a persistant connection and abstract the database layer.

So now I'm using CodeIgniter http://www.codeigniter.com/ which basically accomplishes what I wanted to do in a easier to manage fashion.

Thanks for the other answers.

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