单例工厂连接pdo
嘿伙计们,我在试图理解这一点时遇到了很多麻烦,我只是想知道是否有人可以帮助我解决一些问题。我发现一些应该与 pdo 创建连接的代码。我遇到的问题是在函数中定义我的连接。有人建议全局变量,但随后指出了一个“更好”的解决方案用于数据库连接的全局还是单例?< /a>.我对这段代码的疑问是:
连接工厂的意义是什么? new ConnectionFactory(...) 里面有什么
定义连接时 $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:
What is the point of the connection factory? What goes inside new ConnectionFactory(...)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎对像工厂这样的设计模式主题有点困惑。也许您应该首先阅读一本有关 PHP 的一般设计模式或常见模式的书或一些教程。只需谷歌“php 设计模式”即可。关于这个主题有很多资源。
但简单地回答您的问题:
连接工厂用于独立于实际的底层数据库来生成连接对象。一个简单的 PDO 工厂将根据您传递给它的参数来管理 PDO 所需的 DSN 连接字符串的组装,并返回一个随时可用的 PDO 对象。
在大多数情况下,构建更复杂的数据库适配器类很有用,这些适配器类提供错误处理本身以及更舒适的执行查询方式。同样,工厂类可用于根据您的数据库系统生成正确的连接对象。
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:
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.
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.