设置一个PHP对象全局? [英] Set a PHP object global?
问题描述
我刚开始将我的项目从mysql切换到PDO。在我的项目中,一个新的PDO对象在程序的开始时或多或少地被创建。
$ dbh_pdo = new PDO(mysql:host = $ db_url; dbname = $ db_database_name,$ db_user,$ db_password) ;
现在我想在一些函数和类中使用这个处理程序(是正确的名称吗? 。有没有办法使对象全局变量像变量一样,或者我尝试了一些无法理解的愚蠢的东西,因为在搜索网页时我找不到任何东西...
是的,您可以像对待任何其他变量一样使对象成为全局对象:
$ pdo = new PDO ( '东西');
函数foo(){
global $ pdo;
$ pdo-> prepare('...');
}
您可能还想查看Singleton模式,它基本上是全局,OO风格。
这就是说,我建议你不要使用全局变量。在调试和测试时,它们可能会很痛苦,因为很难分辨修改/使用/访问它是因为什么都可以。他们的用法一般被认为是不好的做法。考虑仔细检查一下你的设计。
我不知道你的应用程序是怎么样的,但是说你是这样做的:
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
}
}
}
您应该这样做:
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
}
}
}
TableCreator
类在这里需要一个PDO对象才能正常工作,所以将一个PDO对象传递给它是非常合理的当创建一个实例。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.
$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 ...
解决方案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.
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.
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 do that 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
TableCreator
class here requires a PDO object to work properly, it makes perfect sense to pass one to it when creating an instance.这篇关于设置一个PHP对象全局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!