zend-framework – 覆盖Zend_Config并访问父节点

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了zend-framework – 覆盖Zend_Config并访问父节点脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我想覆盖Zend_Config方法__set($name,$value),但我有同样的问题.

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

$this->config->something->other->;more = 'crazy VARiable'; // $name in __set() will return 'more'

因为config中的每个节点都是新的Zend_Config()类.

那么 – 如何从覆盖的__set()metod访问父节点名称

我的应用程序:
我必须在控制器中覆盖相同的配置值,但是为了控制覆盖,并且不允许覆盖其他配置变量,我想在相同的其他配置变量中指定覆盖允许的配置密钥的树形数组.

解决方法

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

来自Zend_Config_Ini :: __构造函数()docblock: –

/** The $options parameter may be PRovided as eITher a boolean or an array.
 * If provided as a boolean,this sets the $allowModifications option of
 * Zend_Config. If provided as an array,there are three configuration
 * directives that may be set. For example:
 *
 * $options = array(
 *     'allowModifications' => false,*     'nestSeparator'      => ':',*     'skiPExtends'        => false,*      );
 */
public function __construct($filename,$section = null,$options = false)

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

$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);

结果

回应评论

在这种情况下,您可以简单地扩展Zend_Config_Ini并覆盖__construct()和__set()方法,如下所示: –

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

    public function __construct($filename,$options = false) {
        $this->allowed = array(
            'list','of','allowed','variables'
        );
        parent::__construct($filename,$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-framework – 覆盖Zend_Config并访问父节点全部内容,希望文章能够帮你解决zend-framework – 覆盖Zend_Config并访问父节点所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。