使用PHP PDO的数据库抽象类设计
我正在设计一个 Web 应用程序(实际上,这是一种爱好,我正在尝试自学设计,还有什么比这样做更好的方法呢:)。无论如何,我正在考虑如何处理我的数据库。我对 PDO 很满意,并且我正在考虑在我的抽象类中利用 PDO。我正在考虑创建一个单例,以便只有一个数据库连接。该单例将创建一个 PDO 连接。
在那之后,我不明白为什么我需要做太多其他事情。然后我可以使用数据库处理程序来调用 PDO 函数。我可能需要一些辅助函数,但当它真正实现时,我只会使用 PDO 来执行实际的 SQL 查询。
这种做法有什么问题吗?与我使用过的抽象类相比,它似乎过于简单。
I am in the process of designing a web application (really, it's a hobby, and I'm trying to teach myself design, and what better way is there than doing it :). Anyway, I was thinking about how I would deal with my database. I am comfortable with PDO, and I was thinking of leveraging PDO in my abstraction class. I am thinking of making a singleton, so that there's only one database connection. This singleton would create a PDO connection.
After that, I fail to see why I would need to do too much else. I can then just use the database handler to call PDO functions. I may want some helper functions, but when it gets down to it, I would just use PDO for the actual SQL queries.
Is there something wrong with this approach? It seems overly simple compared to the abstraction classes I've used.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你不需要单例。
数据库单例无法解决任何并发问题。如果有的话,它可以确保您只有一个 PDO 实例用于创建它的请求。而且它提供了全球访问权限,许多人认为这是一件坏事。另外在测试时你必须付出一些额外的努力单例。
只需在引导程序中需要时创建一个延迟连接和存储实例的包装器并设置实例到您的DAL 超类型,例如 TableDataGateway。此外,这样您就不会限制自己只能使用一个 PDO 实例,以防万一您在某个时候需要第二个 PDO 实例。
You don't need the Singleton.
A database Singleton won't solve any concurrency issues. If anything, it can make sure you have only one PDO instance for the request it was created in. And it provides global access, which many people consider a bad thing. In addition you have to make some extra effort when testing the Singleton.
Just create a wrapper that lazy connects and stores the instance when needed in your bootstrap and set the instance to your DAL supertype, for instance a TableDataGateway. Also, this way you don't limit yourself to only one PDO instance in case you will need a second one at some point.
也许你觉得这很简单,因为PDO本质上是一个数据库抽象类。这意味着:工作已经完成。
Maybe it seems so simple to you, because PDO is essentially a database abstraction class. That means: the work is already done.
是的,这是一个好的开始。 PDO + singleton 是一个常用且很棒的组合。由于我个人不喜欢使用单例所涉及的所有类型,因此我编写了一个非常轻量级的数据库类。
与 PDO 相比,它仅引入了两个附加功能: 使用
__callStatic
访问(惰性)PDO 实例(DB::query()
而不是DB::instance() ->query()
) 和两个更容易引用的函数 (DB::q('INSERT INTO table (name) VALUES (?s)', $_POST['insecure_name']))。也许你想两者都看,这真的很方便;)
Yeah, this is a good start. PDO + singleton is an often-used and great combination. As I personally don't like all the typing involved than using singletons, I have written a very lightweight database class.
It introduces only two additional features over PDO: Access of (lazy) PDO instance using
__callStatic
(DB::query()
instead ofDB::instance()->query()
) and two functions for easier quoting (DB::q('INSERT INTO table (name) VALUES (?s)', $_POST['insecure_name'])
). Maybe you want to look at both, it's really handy ;)您可能还对 php-pdo-wrapper-class。它是一个轻量级数据库类,扩展了 PDO,添加了多种方法 - 插入、更新、删除、选择(以及其他一些方法) - 用于简化常见的 SQL 语句。我在我的开发中使用过这个项目,并且强烈推荐。
You may also be interested in the project php-pdo-wrapper-class. It's a light-weight database class that extends PDO, adding several methods - insert, update, delete, select (and a few others) - for simplifying common SQL statements. I've used this project in my development and would highly recommend.