4

I want to overwrite Zend_Config method __set($name, $value), but I have same problem.

$name - return current key of overwrite config value, eg:

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

Because every node in config is new Zend_Config() class.

So - how from overwritten __set() metod get access to the parents nodes names?

My application: I must overwrite same config value in controller, but to have controll about the overwritting, and do not allow to overwrite other config variables, I want to specify in same other config variable, an tree-array of overwriting allowed config keys.

BlueMan
  • 687
  • 2
  • 10
  • 23

2 Answers2

3

Zend_Config is read only unless you have set $allowModifications to true during construction.

From the 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)

This means that you would need to do something like this:-

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

Result

string 'test' (length=4)

In response to comment

In that case you can simply extend Zend_Config_Ini and override the __construct() and __set() methods like this:-

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.
    }
}
vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • I know about it very well, but I dont want to allow modification to whole variables in config, but only for specify variables. So I must overload Zend_Config, and manualy filter which variable i want to overwrite, which not. – BlueMan Feb 17 '12 at 11:21
  • In that case see my addition above – vascowhite Feb 17 '12 at 12:28
  • ah, I see your problem now. The object returned is always a Zend_Config!! My 2nd solution won't work. I'll try again :) – vascowhite Feb 17 '12 at 12:40
0

Always there is another way :)

$arrSettings = $oConfig->toArray();
$arrSettings['params']['dbname'] = 'new_value';
$oConfig= new Zend_Config($arrSettings);
Andron
  • 6,413
  • 4
  • 43
  • 56