Zend framework 覆盖Zend_配置并访问父节点

Zend framework 覆盖Zend_配置并访问父节点,zend-framework,config,overwrite,Zend Framework,Config,Overwrite,我想覆盖Zend_配置方法uu集($name,$value),但我有同样的问题 $name-返回覆盖配置值的当前键,例如: $this->config->something->other->more = 'crazy variable'; // $name in __set() will return 'more' 因为config中的每个节点都是新的Zend_config()类 那么-如何从overwrited\uuu set()获取对父节点名称的访问权限 我的申请:

我想覆盖Zend_配置方法uu集($name,$value),但我有同样的问题

$name-返回覆盖配置值的当前键,例如:

$this->config->something->other->more = 'crazy variable'; // $name in __set() will return 'more'
因为config中的每个节点都是新的Zend_config()类

那么-如何从overwrited\uuu set()获取对父节点名称的访问权限

我的申请:
我必须在控制器中覆盖相同的配置值,但要控制覆盖,并且不允许覆盖其他配置变量,我想在相同的其他配置变量中指定,覆盖允许的配置键的树数组。

Zend_config是只读的,除非您在构造期间将$allowModifications设置为true

Zend\u Config\u Ini::\u构造函数()
:-

这意味着您需要执行以下操作:-

$inifile = APPLICATION_PATH . '/configs/application.ini';
$section = 'production';
$allowModifications = true;
$config = new Zend_Config_ini($inifile, $section, $allowModifications);
$config->resources->db->params->username = 'test';
var_dump($config->resources->db->params->username);
结果

字符串“测试”(长度=4)

回应评论

在这种情况下,您只需扩展Zend_Config_Ini并覆盖
\uu构造()
\uu集()
方法,如下所示:-

class Application_Model_Config extends Zend_Config_Ini
{
    private $allowed = array();

    public function __construct($filename, $section = null, $options = false) {
        $this->allowed = array(
            'list',
            'of',
            'allowed',
            'variables'
        );
        parent::__construct($filename, $section, $options);
    }
    public function __set($name, $value) {
        if(in_array($name, $this->allowed)){
            $this->_allowModifications = true;
            parent::__set($name, $value);
            $this->setReadOnly();
        } else { parent::__set($name, $value);} //will raise exception as expected.
    }
}

总是有另一种方法:)


我对此非常了解,但我不想允许修改配置中的整个变量,只允许修改指定变量。所以我必须重载Zend_Config,并手动筛选我想要覆盖的变量,而不是。在这种情况下,请看我上面的添加。啊,我现在看到你的问题了。返回的对象始终是Zend_配置!!我的第二个解决方案行不通。我会再试一次:)
class Application_Model_Config extends Zend_Config_Ini
{
    private $allowed = array();

    public function __construct($filename, $section = null, $options = false) {
        $this->allowed = array(
            'list',
            'of',
            'allowed',
            'variables'
        );
        parent::__construct($filename, $section, $options);
    }
    public function __set($name, $value) {
        if(in_array($name, $this->allowed)){
            $this->_allowModifications = true;
            parent::__set($name, $value);
            $this->setReadOnly();
        } else { parent::__set($name, $value);} //will raise exception as expected.
    }
}
$arrSettings = $oConfig->toArray();
$arrSettings['params']['dbname'] = 'new_value';
$oConfig= new Zend_Config($arrSettings);