将 PHP 对象设置为全局?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4696189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:00:15  来源:igfitidea点击:

Set a PHP object global?

phppdoglobal-variables

提问by wowpatrick

I just started switching my project form the mysql to PDO. In my project a new PDO Object is created more or less right a the beginning of the programm.

我刚开始将我的项目从 mysql 切换到 PDO。在我的项目中,或多或少地在程序开始时创建了一个新的 PDO 对象。

$dbh_pdo = new PDO("mysql:host=$db_url;dbname=$db_database_name", $db_user, $db_password);

Now I would like to use this handler (is that the correct name?) in some functions and classes. Is there a way to make objects global just like variables or am I trying something unspeakably stupid, because I couldn't find anything when searching the web ...

现在我想在一些函数和类中使用这个处理程序(这是正确的名称吗?)。有没有办法让对象像变量一样全局化,或者我是否在尝试一些难以形容的愚蠢行为,因为我在搜索网络时找不到任何东西......

回答by netcoder

Yes, you can make objects global just like any other variable:

是的,您可以像任何其他变量一样使对象成为全局对象:

$pdo = new PDO('something');
function foo() {
   global $pdo;
   $pdo->prepare('...');
}

You may also want to check out the Singleton pattern, which basically is a global, OO-style.

您可能还想查看 Singleton 模式,它基本上是一种全局的 OO 风格。

That being said, I'd recommend you not to use globals. They can be a pain when debugging and testing, because it's hard to tell who modified/used/accessed it because everything can. Their usage is generally considered a bad practice. Consider reviewing your design a little bit.

话虽如此,我建议您不要使用 globals。在调试和测试时,它们可能会很痛苦,因为很难分辨谁修改/使用/访问了它,因为一切都可以。它们的使用通常被认为是一种不好的做法。考虑稍微一下您的设计。

I don't know how your application looks like, but say you were doing this:

我不知道你的应用程序是什么样子,但说你是这样做的:

class TableCreator {
   public function createFromId($id) {
       global $pdo;
       $stmt = $pdo->prepare('SELECT * FROM mytable WHERE id = ?');
       $stmt->execute(array($id));
       $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
       foreach ($rows as $row) {
           // do stuff
       }
   }
}

You should dothat instead:

应该这样做

class TableCreator {
   protected $pdo;

   public function __construct(PDO $pdo) {
       $this->pdo = $pdo;
   }

   public function createFromId($id) {
       $stmt = $this->pdo->prepare('SELECT * FROM mytable WHERE id = ?');
       $stmt->execute(array($id));
       $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
       foreach ($rows as $row) {
           // do stuff
       }
   }
}

Since the TableCreatorclass here requires a PDO object to work properly, it makes perfect sense to pass one to it when creating an instance.

由于TableCreator这里的类需要一个 PDO 对象才能正常工作,因此在创建实例时将一个传递给它是非常有意义的。

回答by Jeff Hubbard

You'll use $GLOBALS['dbh_pdo']instead of $dbh_pdoinside any functions. Or you can use the global keyword, and use $dbh_pdo(i.e. global $dbh_pdo).

您将在任何函数中使用$GLOBALS['dbh_pdo']而不是使用$dbh_pdo。或者您可以使用 global 关键字,并使用$dbh_pdo(ie global $dbh_pdo)。

回答by Jeremy

You could also try using a Singleton to pass back a PDO object to you. That way you only ever have one PDO object (and one database connection) in any request which saves on memory/server resources.

您也可以尝试使用 Singleton 将 PDO 对象传回给您。这样一来,您在任何请求中都只有一个 PDO 对象(和一个数据库连接),从而节省了内存/服务器资源。