加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 16:43:26 所属栏目:PHP教程 来源:网络整理
导读:我想覆盖Zend_Config方法__set($name,$value),但我有同样的问题. $name – 返回覆盖配置值的当前键,例如: $this-config-something-other-more = 'crazy variable'; // $name in __set() will return 'more' 因为config中的每个节点都是新的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);

结果

string ‘test’ (length=4)

回应评论

在这种情况下,您可以简单地扩展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.
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读