我有一個負責處理所有配置的類 - 從文件讀取配置,並獲取在index.php(+從那裏設置它們)中設置的基本配置變量。因此,我決定在這裏使用多態 - 我做了Config類抽象,並用FILE和VARIABLE類擴展了這個。多態 - 確保一切正確
這是一個很好的做法行爲,如果這兩個職責的基類是100行長嗎?
不要在這裏冷落我 - 我只是不想在項目已經完成時發現它不是靈活的解決方案。
這是代碼(雖然沒有重構,測試和添加一些功能,但概念應該清楚)。
class Config {
private $file;
public static $configs = array();
/**
* Initializes basic website configurations such as base URL, or the name
* of the index file.
*
* These values can be accessed through this class
*/
public static function init($configs = array())
{
foreach($configs as $key => $value)
{
self::$configs[$key] = $value;
}
}
/**
* Returns the configuration variable which is set in the index file
*
* @param string $attribute
* @return multitype:
*/
public function __get($attribute)
{
return ($this->configs[$attribute]) ? $this->configs[$attribute] : -1;
}
/**
* Setting path to the config file.
*
* @param string $module
*/
private function __construct($module)
{
// Path to the config file
$path = APATH . 'config' . DIRECTORY_SEPARATOR . $module . '.php';
// Set the config file to the attribute here
$this->file = include $path;
}
/**
* Return the object.
*
*/
public static function factory($module)
{
return new Config($module);
}
/**
* Loads configurations from the given file.
*
*/
public function load($property)
{
// Return requested value
return $array[$property];
}
}
請發一些代碼嗎? – mauris
http://codereview.stackexchange.com可能是比較合適的網站。 –
誰最喜歡的問題是?的xD – user1615069