所以我知道为了在b.class.php中使用a.class.php,我需要在b类文件中包含a类。我的问题是。我现在在我的网站平台上有两个类文件。db.class.php和account.class.php
db.class.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/settings.php');
class db extends pdo{
//Website Variables
public $sitedb = '';
public $siteconfig;
public $sitesettings = array(
'host' => SITEHOST,
'database' => SITEDB,
'username' => SITEUSER,
'password' => SITEPASS,
);
public $realmdb = '';
public $realmconfig;
public $realmsettings = array(
'host' => REALMHOST,
'database' => REALMDB,
'username' => REALMUSER,
'password' => REALMPASS,
);
public function __construct(){
$this->sitedb = new PDO(
"mysql:host={$this->sitesettings['host']};" .
"dbname={$this->sitesettings['database']};" .
"charset=utf8",
"{$this->sitesettings['username']}",
"{$this->sitesettings['password']}"
);
$this->realmdb = new PDO(
"mysql:host={$this->realmsettings['host']};" .
"dbname={$this->realmsettings['database']};" .
"charset=utf8",
"{$this->realmsettings['username']}",
"{$this->realmsettings['password']}"
);
$this->sitedb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$this->realmdb->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
}
}
$db = new db();
account.class.php
<?php
require_once($_SERVER['DOCUMENT_ROOT'].'/settings.php');
class account extends db {
public function Login() {
$query = <<<SQL
SELECT id
FROM profile
WHERE password = :password
SQL;
$resource = $db->sitedb->prepare ( $query );
$resource->execute( array(
':password' => sha1(strtoupper($_POST['email'].':'.$_POST['password'],
));
$row_count = $resource->rowCount();
echo $row_count;
}
}
$account = new account();
这个当前的格式告诉我,数据库不能被重新定义,但是,如果我删除包含具有的设置文件的要求
foreach (glob("functions.d/*.class.php") as $class)
{
include $class;
}
然后它告诉我找不到类db。我可以用什么方法来正确地完成这项工作?
事实证明,实现这一目标的最佳方法是正确地保留include,并将所有类放在同一文件中,在我的include页面中,使用类的类具有全局$db或全局$account,这取决于我调用的I class函数。