我有一个班级配置。每当'localhost'
位于$_SERVER['HTTP_HOST']
时,我希望$ db_host为'localhost'
。默认情况下,它应为'defaulthost'
;
class Config {
public static $db_username = 'username';
public static $db_password = 'password';
public static $db_database = 'database';
public function __construct() {
$host = 'defaulthost';
if(stristr($_SERVER['HTTP_HOST'],'localhost')){
$host = 'localhost';
};
self::$db_host = $host;
}
}
此代码给我一个错误
Fatal error: Access to undeclared static property: Config::$db_host in /Users/aakilfernandes/Dropbox/Projects/codeharmony/scripts/dev/v02/class/Database.php on line 98
答案 0 :(得分:2)
您只需要声明属性$ db_host。
class Config {
public static $db_username = 'username';
public static $db_password = 'password';
public static $db_database = 'database';
public static $db_host = 'defaulthost';
public function __construct() {
if (stristr($_SERVER['HTTP_HOST'], 'localhost')){
self::$db_host = 'localhost';
}
}
}
答案 1 :(得分:0)
您应该像使用db_database ;-)
一样初始化db_host