PHP:将数据库层实现为单例可以接受吗?里面的代码

发布于 2024-10-17 17:13:43 字数 992 浏览 6 评论 0原文

我知道单身人士很糟糕。但这也有坏处吗?

class DaoMySQL {

    private static $instance;
    private $PDO;

    private function __construct() {
        $this->PDO = new PDO('mysql:dbname='.MYSQL_DEFAULT_DATABASE.';host='.MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
        $this->PDO->query('SET NAMES \'utf8\'');
    }

    /**
     * @return DaoMySQL
     */
    static public function singleton() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c();
        }
        return self::$instance;
    }

    /**
     * @return PDO
     */
    public function getPDO() {
        return $this->PDO;
    }

}

为了使用它,我做了这样的事情。 (这是来自我的 Bean 类,所有数据对象都扩展了它。)

public function delete() {
    $calledClassName = get_called_class();
    $query = "DELETE FROM `" . $calledClassName::table . "` WHERE `id` = $this->id";
    return DaoMySQL::singleton()->getPDO()->exec($query);
}

I know singletons are bad. But is it bad for this, too?

class DaoMySQL {

    private static $instance;
    private $PDO;

    private function __construct() {
        $this->PDO = new PDO('mysql:dbname='.MYSQL_DEFAULT_DATABASE.';host='.MYSQL_HOSTNAME, MYSQL_USERNAME, MYSQL_PASSWORD);
        $this->PDO->query('SET NAMES \'utf8\'');
    }

    /**
     * @return DaoMySQL
     */
    static public function singleton() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c();
        }
        return self::$instance;
    }

    /**
     * @return PDO
     */
    public function getPDO() {
        return $this->PDO;
    }

}

To use this, I do something like this. (This is from my Bean class which all data objects extends.)

public function delete() {
    $calledClassName = get_called_class();
    $query = "DELETE FROM `" . $calledClassName::table . "` WHERE `id` = $this->id";
    return DaoMySQL::singleton()->getPDO()->exec($query);
}

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

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

发布评论

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

评论(2

很酷不放纵 2024-10-24 17:13:43

许多人开始使用依赖注入容器来管理他们的对象,而不是使用单例。也许值得一看?然后您需要确保对象可以访问容器。您可以从那里获取其他所有内容。

就我个人而言,我使用 Symfony 组件中的 sfServiceContainer 。它是一个独立的 DI 容器,最近似乎很流行。

更新

不需要使用框架或库。 Fabien Potencier 的有关依赖注入的文章应该能让您有足够的掌握DI 来实现您自己的。但为什么要重新发明轮子呢?不使用良好的现有库有点NIH的味道。

请注意,除了我使用的 sfServiceContainer 之外,还有许多其他 DI 库。另请注意,sfServiceContainer 是一个完全独立的库。它不需要 Symfony 或任何其他框架。它所需要的只是普通的旧 PHP。

Many people are starting to use Dependency Injection containers to manage their objects instead of using singletons. Perhaps it's worth a look? Then all you need to ensure is that objects can access the container. You can fetch everything else from there.

Personally I use sfServiceContainer from Symfony Components. It's a stand-alone DI container and seems quite popular these days.

Update

You don't need to use a framework or a library. Fabien Potencier's articles on dependency injection should give you a good enough grasp of DI to implement your own. But why reinvent the wheel? Not using a good, existing library smells of NIH.

Note that there are many other DI libraries besides the sfServiceContainer that I use. Also note that sfServiceContainer is a completely stand-alone library. It does not need Symfony or any other framework. All it requires is plain old PHP.

云柯 2024-10-24 17:13:43

单例的问题(好吧,其中之一)是应用程序应该真正负责确定对象的生命周期。

阅读 Steve Yegge 的文章Singleton 被认为是愚蠢的

The thing (well, one of them) that's wrong with Singletons is that the application should really be responsible for determining an object's life-cycle.

Have a read of Steve Yegge's article Singleton Considered Stupid

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