【问题标题】:Overwrite Zend_Config and access parents nodes覆盖 Zend_Config 并访问父节点
【发布时间】:2012-03-07 22:27:55
【问题描述】:

我想覆盖 Zend_Config 方法 __set($name, $value),但我有同样的问题。

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

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

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

那么 - 如何从覆盖的 __set() 方法中访问父节点名称?

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

【问题讨论】:

    标签: zend-framework config overwrite


    【解决方案1】:

    总是有另一种方式:)

    $arrSettings = $oConfig->toArray();
    $arrSettings['params']['dbname'] = 'new_value';
    $oConfig= new Zend_Config($arrSettings);
    

    【讨论】:

      【解决方案2】:

      Zend_Config 是只读的,除非你在构建过程中将 $allowModifications 设置为 true。

      来自Zend_Config_Ini::__constructor()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);
      

      结果

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

      回应评论

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

      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_Config!!我的第二个解决方案不起作用。我会再试一次:)
      猜你喜欢
      • 1970-01-01
      • 2011-01-11
      • 2021-04-19
      • 1970-01-01
      • 2014-05-31
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多