2014-02-06 64 views
0

我有一個Config類。每當'localhost'$_SERVER['HTTP_HOST'],我想$ db_host是'localhost'。默認情況下,它應該是'defaulthost';如何製作動態PHP類

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

所以......'公共靜態$ DB_HOST做;'應該解決這個問題了...... –

回答

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'; 
     } 
    } 
} 
0

你應該初始化DB_HOST以同樣的方式像你db_database ;-)