这是我一般的PHP页面:使用以前在其他类中声明的对象
<?php
require_once('includes.php');
require_once('cms.class.php');
.....
rest of the page
?>
在
includes.php
的亲对象调用$ DB开始,我想在cms.class.php
includes.php:
$db = new PDO('mysql:host=localhost;dbname=xxxxx','xxxxx','xxxxxx');
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
我如何使用我的班这个数据库对象,而不必多个地方,我的凭据存储?
通过cms类构造函数在您的类中注入'$ db'对象,或者创建一个您传递的注册表对象。 – 2012-04-13 18:23:05
这是完整的includes.php吗?我的理解是,任何“内联”都将被执行。因此,$ db应该是可访问的。 – kevingreen 2012-04-13 18:23:17