Reputation: 707
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.
Upvotes: 4
Views: 1801
Reputation: 6631
Always there is another way :)
$arrSettings = $oConfig->toArray();
$arrSettings['params']['dbname'] = 'new_value';
$oConfig= new Zend_Config($arrSettings);
Upvotes: 0
Reputation: 18440
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.
}
}
Upvotes: 3