用于管理和包装多个数据库对象的单例工厂

发布于 2024-09-10 08:57:52 字数 712 浏览 6 评论 0原文

我正在构建一个 PHP 应用程序,它必须与几个结构相同的数据库进行交互。我想使用单例工厂来分发与数据库的连接并最大程度地减少重复连接的数量。我还想用一些函数包装数据库类。

如果我可以在一个班级中完成所有这些工作,那就非常方便了。我尝试使用单例工厂(当时这似乎是个好主意),却发现似乎必须返回其他类才有用。有没有一种简单的方法来组合单例工厂和数据库包装功能,或者我应该将数据库包装函数放在另一个类中?

static private $instance = array();

private function __construct($name) {
    switch ($name) {
        //select db connection
    }

    $this->db = $this->getDb();
    return;
}

protected function __clone() {

}

public static function singleton($name) {
    if (!isset(self::$instance[$name])) {
        $c = __CLASS__;
        self::$instance[$name] = new $c($name);
    }
    return self::$instance[$name];
}

public function wrapperFunction() {
    //stuff
}

I'm building a PHP application which has to interact with several structurally identical databases. I'd like to use a singleton factory to hand out connections to the databases and minimize the number of duplicate connections. I'd also like to wrap the database class with a few functions.

It would be extremely convenient if I could do all this in a single class. I tried using a singleton factory (it seemed like a good idea at the time), only to realize that it seems like has to return other classes to be useful. Is there an easy way to combine the singleton factory and database wrapping functionality, or should I just put the database wrapping functions in another class?

static private $instance = array();

private function __construct($name) {
    switch ($name) {
        //select db connection
    }

    $this->db = $this->getDb();
    return;
}

protected function __clone() {

}

public static function singleton($name) {
    if (!isset(self::$instance[$name])) {
        $c = __CLASS__;
        self::$instance[$name] = new $c($name);
    }
    return self::$instance[$name];
}

public function wrapperFunction() {
    //stuff
}

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

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

发布评论

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

评论(2

一身骄傲 2024-09-17 08:57:52

我经常做类似的事情(例如,按名称管理多个数据库连接)。我建议的一项更改是使构造函数和 $instance 数组受到保护。原因是,以这种方式进行测试变得更加容易(因为您可以使用包装类来扩展它,以便能够根据需要访问它们并创建和销毁实例)。当然,这为某人在应用程序中执行此操作提供了可能性,但为什么要放弃从子类管理实例的可能性呢?

只是我的 0.02 美元...

I do something similar quite often (Managing multiple database connections by name for example). The one change I'll suggest is making the constructor and $instance array protected. The reason is that it becomes FAR easier to test that way (since you can extend it with a wrapper class to be able to access them and create and destroy instances as you need to). Sure, that opens up the possibility for someone to do that in the application, but why throw away the possibility of managing instances from a child class?

Just my $0.02...

千と千尋 2024-09-17 08:57:52

看起来这可能是通过使用一些类范围的状态变量并使用 func_num_argsfunc_get_arg< /a> 在构造函数中。

然而,为了减少每分钟 WTF 数量,我会去GoalBased 建议将事物分为两类。

It looks like this would probably be possible by using some class-wide state variables and doing some clever things with func_num_args and func_get_arg in the constructor.

However, in the interest of reducing the number of WTFs per minute, I'll go with GoalBased's suggestion of splitting things up into two classes.

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