Php 如何创建可升级的全局配置文件

Php 如何创建可升级的全局配置文件,php,Php,导言 我正在创建自己的框架,实际上一切都很好。但我需要对这种情况发表意见。所以,我想创建一个可以通过代码升级的配置文件,所谓“upgradable”,我的意思是我可以在运行的应用程序上更改配置。检查一个例子,我有一个默认的config.php文件,它是我的框架的基本配置,这个文件位于根文件夹中。结构如下: application <- APP file system <- Framework file config.php <- Fra

导言

我正在创建自己的框架,实际上一切都很好。但我需要对这种情况发表意见。所以,我想创建一个可以通过代码升级的配置文件,所谓“upgradable”,我的意思是我可以在运行的应用程序上更改配置。检查一个例子,我有一个默认的
config.php
文件,它是我的框架的基本配置,这个文件位于根文件夹中。结构如下:

application      <- APP file
system           <- Framework file
config.php       <- Framework base configuration
index.php
.htaccess
class Config
{
    const Language = 'english';
    const DB_HOST  = 'localhost';
}
$config['LANGUAGE'] = defined('Config::Language') ? Config::LANGUAGE : 'english';
class Settings
{
    private static $_config = array();

    function __construct()
    {
        $file = 'application/config/config.php';
        include $file;
        self::$_config = $config;
    }

    //set or update an item to the global config file
    public static function setItem($name, $value)
    {
        self::$_config[$name] = $value;
    }

    //get a settings from the global config file
    public static function getItem($name)
    {
        if(isset(self::$_config[$name]))
        {
             return self::$_config[$name];
        }
    }
}
echo Settings::getItem('LANGUAGE');
Settings::setItem('LANGUAGE', 'italian');
echo Settings::getItem('LANGUAGE');
在这个文件中,正如我所说,我已经得到了框架的基本配置。现在在我的
应用程序
文件夹中有一个目录,其中包含另一个
config.php
文件,是应用程序的一部分

此文件包含一个数组,如下所示:

application      <- APP file
system           <- Framework file
config.php       <- Framework base configuration
index.php
.htaccess
class Config
{
    const Language = 'english';
    const DB_HOST  = 'localhost';
}
$config['LANGUAGE'] = defined('Config::Language') ? Config::LANGUAGE : 'english';
class Settings
{
    private static $_config = array();

    function __construct()
    {
        $file = 'application/config/config.php';
        include $file;
        self::$_config = $config;
    }

    //set or update an item to the global config file
    public static function setItem($name, $value)
    {
        self::$_config[$name] = $value;
    }

    //get a settings from the global config file
    public static function getItem($name)
    {
        if(isset(self::$_config[$name]))
        {
             return self::$_config[$name];
        }
    }
}
echo Settings::getItem('LANGUAGE');
Settings::setItem('LANGUAGE', 'italian');
echo Settings::getItem('LANGUAGE');
因此,我只需检查在base
config
文件中是否设置了特定的常量

此文件由我的
设置
类使用,该类是我的框架类的一部分

问题

我的所有请求都来自
index.php
,在.htaccess中我有我的规则
RewriteRule^(+)$index.php?url=$1[QSA,L]

因此,如果我更改config item的值,在
应用程序/config/config.php
中,我可以看到从当前实例更改的值,但是如果我重新加载页面,例如,我会在
应用程序
文件夹的
config.php
中设置默认值。发生这种情况的原因是每次从以下位置声明
设置
类的实例:

index.php -> Framework load instance [Settings, Controller, Models, etc...] -> Logic Business
因此,您可以看到每次刷新时我都丢失了编辑。 我的
设置
类如下所示:

application      <- APP file
system           <- Framework file
config.php       <- Framework base configuration
index.php
.htaccess
class Config
{
    const Language = 'english';
    const DB_HOST  = 'localhost';
}
$config['LANGUAGE'] = defined('Config::Language') ? Config::LANGUAGE : 'english';
class Settings
{
    private static $_config = array();

    function __construct()
    {
        $file = 'application/config/config.php';
        include $file;
        self::$_config = $config;
    }

    //set or update an item to the global config file
    public static function setItem($name, $value)
    {
        self::$_config[$name] = $value;
    }

    //get a settings from the global config file
    public static function getItem($name)
    {
        if(isset(self::$_config[$name]))
        {
             return self::$_config[$name];
        }
    }
}
echo Settings::getItem('LANGUAGE');
Settings::setItem('LANGUAGE', 'italian');
echo Settings::getItem('LANGUAGE');
实践中的问题

该类在脚本中使用非常简单,为了使用它,我可以执行以下操作:

application      <- APP file
system           <- Framework file
config.php       <- Framework base configuration
index.php
.htaccess
class Config
{
    const Language = 'english';
    const DB_HOST  = 'localhost';
}
$config['LANGUAGE'] = defined('Config::Language') ? Config::LANGUAGE : 'english';
class Settings
{
    private static $_config = array();

    function __construct()
    {
        $file = 'application/config/config.php';
        include $file;
        self::$_config = $config;
    }

    //set or update an item to the global config file
    public static function setItem($name, $value)
    {
        self::$_config[$name] = $value;
    }

    //get a settings from the global config file
    public static function getItem($name)
    {
        if(isset(self::$_config[$name]))
        {
             return self::$_config[$name];
        }
    }
}
echo Settings::getItem('LANGUAGE');
Settings::setItem('LANGUAGE', 'italian');
echo Settings::getItem('LANGUAGE');
输出将是:

english
italian
但是如果我刷新页面并且不再调用
setItem
,我甚至会看到
english
被声明为默认值。这将删除该类设计的所有逻辑