PDO 全局实例 新的 PDO ,它可能吗?
问题描述
我使用 PHP PDO 连接到我的数据库并运行一些查询,然后在某些表单上使用查询返回.
I am using PHP PDO to connect to my database and run some querys to then use the query return on some forms.
也就是说,我有一个选择,它由来自查询的值填充.
That is, I have a select where it is populated by the values coming from the query.
我创建了两个函数来连接数据库和数据,但我想知道我是否可以创建一个全局变量,因为我使用的是New PDO".
I created two functions to connect to the database and data, but I wanted to know if I can create a global variable because I am using the "New PDO" .
推荐答案
您可以以简单的旧程序风格非常简单地共享"同一个数据库连接对象 (PDO) :-) 这是一个简单的例子:
You could "share" the same database connection object (PDO) very simple in plain old procedural style :-) Here is a simple example:
用法
如果您更喜欢更专业的解决方案,请查看 PHP-DI 并使用依赖注入.
If you prefer a more professional solution then take a look an PHP-DI and use dependency injection.
这篇关于PDO 全局实例 新的 PDO ,它可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!