单例工厂连接pdo

发布于 2024-09-02 02:49:56 字数 945 浏览 4 评论 0原文

嘿伙计们,我在试图理解这一点时遇到了很多麻烦,我只是想知道是否有人可以帮助我解决一些问题。我发现一些应该与 pdo 创建连接的代码。我遇到的问题是在函数中定义我的连接。有人建议全局变量,但随后指出了一个“更好”的解决方案用于数据库连接的全局还是单例?< /a>.我对这段代码的疑问是:

  1. 连接工厂的意义是什么? new ConnectionFactory(...) 里面有什么

  2. 定义连接时 $db = new PDO(...);为什么没有 try 或 catch (我用它们来处理错误)?这是否意味着我必须对每个后续查询使用 try 和 catch ?

这是代码:

class ConnectionFactory
{
    private static $factory;
    public static function getFactory()
    {
        if (!self::$factory)
            self::$factory = new ConnectionFactory(...);
        return self::$factory;
    }

    private $db;

    public function getConnection() {
        if (!$db)
            $db = new PDO(...);
        return $db;
    }
}

function getSomething()
{
    $conn = ConnectionFactory::getFactory()->getConnection();
    .
    .
    .
}

Hey guys I am having a lot of trouble trying to understand this and I was just wondering if someone could help me with some questions. I found some code that is supposed to create a connection with pdo. The problem I was having was having my connection defined within functions. Someone suggested globals but then pointed to a 'better' solution Global or Singleton for database connection?. My questions with this code are:

  1. What is the point of the connection factory? What goes inside new ConnectionFactory(...)

  2. When the connection is defined $db = new PDO(...); why is there no try or catch (I use those for error handling)? Does this then mean I have to use try and catch for every subsequent query?

Here's the code:

class ConnectionFactory
{
    private static $factory;
    public static function getFactory()
    {
        if (!self::$factory)
            self::$factory = new ConnectionFactory(...);
        return self::$factory;
    }

    private $db;

    public function getConnection() {
        if (!$db)
            $db = new PDO(...);
        return $db;
    }
}

function getSomething()
{
    $conn = ConnectionFactory::getFactory()->getConnection();
    .
    .
    .
}

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

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

发布评论

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

评论(1

情丝乱 2024-09-09 02:49:56

您似乎对像工厂这样的设计模式主题有点困惑。也许您应该首先阅读一本有关 PHP 的一般设计模式或常见模式的书或一些教程。只需谷歌“php 设计模式”即可。关于这个主题有很多资源。

但简单地回答您的问题:

  1. 连接工厂用于独立于实际的底层数据库来生成连接对象。一个简单的 PDO 工厂将根据您传递给它的参数来管理 PDO 所需的 DSN 连接字符串的组装,并返回一个随时可用的 PDO 对象。

  2. 在大多数情况下,构建更复杂的数据库适配器类很有用,这些适配器类提供错误处理本身以及更舒适的执行查询方式。同样,工厂类可用于根据您的数据库系统生成正确的连接对象。

You seem a little confused about the topic of design patterns like the factory. Maybe you should read a book or some tutorials about design patterns in general or common patterns for PHP first. Just google "php design patterns". There are plenty of resources out there on that subject.

But to answer your question briefly:

  1. A Connection Factory is used to produce connection objects independently of the actual underlying database. A simple PDO factory would manage the assembly of the DSN connection strings that PDO needs based on the parameters you pass to it and return a ready to use PDO object.

  2. In most cases it is useful to build more sophisticated database adapter classes that provide error handling themselves along with more comfortable ways of executing queries. Again, a factory class can then be used to produce the correct connection object according to your database system.

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