Edw*_*uay 41 php configuration
我经常在.NET和PHP开发之间切换.使用ASP.NET站点,我在web.config文件中保存配置信息(例如连接字符串,目录,应用程序设置),该文件受到适当保护并且易于访问值等.
在PHP中,我用一个为每个变量都有静态方法的类来解决这个问题:
class webconfig {
public static function defaultPageIdCode() {
return 'welcome';
}
}
Run Code Online (Sandbox Code Playgroud)
app变量包含的文件是通过一行访问的:
$dp = webconfig::defaultPageIdCode();
Run Code Online (Sandbox Code Playgroud)
而且由于PHP没有编译,因此很容易telnet并更改网站的值,所以这个解决方案工作得相当好,并给我这两个优点:
但我可以想象,人们可以通过其他方式解决在PHP中保存Web配置设置的问题,这可能具有其他优势.
特别是那些有许多PHP框架经验的人,还有哪些方法可以保存配置变量及其优缺点?
rav*_*ren 35
我决定列出所有已知方法及其优缺点.
我将此答案标记为社区维基,因此协作更容易.
define('CONFIG_DIRECTIVE', 'value');$object = new MyObject(CONFIG_DIRECTIVE);例如:XML,INI,YAML等.
config_directive = value.)parse_ini_file().)$config['directive'] = 'value';$object = new MyObject($config['directive']);$container = new MyContainer($config);myCfgObj::setDirective('DIRECTIVE', 'value');myCfgObj->setDirective('DIRECTIVE', 'value');$object = new MyObject(myCfgObj::getDirective('DIRECTIVE'));$object = new MyObject(myCfgObj->getDirective('DIRECTIVE'));Rob*_*itt 24
我倾向于在PHP中使用Settings静态类,这是因为
例:
abstract class Settings
{
static private $protected = array(); // For DB / passwords etc
static private $public = array(); // For all public strings such as meta stuff for site
public static function getProtected($key)
{
return isset(self::$protected[$key]) ? self::$protected[$key] : false;
}
public static function getPublic($key)
{
return isset(self::$public[$key]) ? self::$public[$key] : false;
}
public static function setProtected($key,$value)
{
self::$protected[$key] = $value;
}
public static function setPublic($key,$value)
{
self::$public[$key] = $value;
}
public function __get($key)
{//$this->key // returns public->key
return isset(self::$public[$key]) ? self::$public[$key] : false;
}
public function __isset($key)
{
return isset(self::$public[$key]);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在运行时,如果先加载此文件,然后加载数据库配置文件,则数据库配置文件将如下所示:
<?php
Settings::setProtected('db_hostname', 'localhost');
Settings::setProtected('db_username', 'root');
Settings::setProtected('db_password', '');
Settings::setProtected('db_database', 'root');
Settings::setProtected('db_charset', 'UTF-8');
//...
echo Settings::getProtected('db_hostname'); // localhost
//...
Settings::setPublic('config_site_title', 'MySiteTitle');
Settings::setPublic('config_site_charset', 'UTF-8');
Settings::setPublic('config_site_root', 'http://localhost/dev/');
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,我们有一个__get只允许获取公共变量的方法,我们为什么这样做的一个例子如下:
$template = new Template();
$template->assign('settings', new Settings());
Run Code Online (Sandbox Code Playgroud)
无论我们将此对象用作静态对象,我们现在可以在模板中使用这些值.
<html>
<head>
<?php echo isset($settings->config_site_title) ? $settings->config_site_title : 'Fallback Title'; ?>
</head>
</html>
Run Code Online (Sandbox Code Playgroud)
这只允许您在初始化期间访问公共数据.
这可能会变得更复杂但更加系统友好,例如:
loadConfig自动解析配置文件的方法,xml,php,yaml.shutdown_function,则可以使用新设置自动更新数据库.这也是我迄今为止完成这项工作的最佳方法.
我这样做的方法是直接将它们存储在一个array文件中并将文件保存为config.php
<?php
$config['dbname'] = "mydatabase";
$config['WebsiteName'] = "Fundoo Site";
$config['credits'] = true;
$config['version'] = "4.0.4";
?>
Run Code Online (Sandbox Code Playgroud)
这是大多数PHP框架如Wordpress等的方式.