Sunday, March 15, 2009

Zend framework source code learning - Zend_Config

Zend_Config class

To understand this class, we only need to look through its 3 methods.
The first is, naturally, its constructor.

public function __construct(array $array, $allowModifications = false)
$this->_allowModifications = (boolean) $allowModifications;
$this->_loadedSection = null;
$this->_index = 0;
$this->_data = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$this->_data[$key] = new self($value, $this->_allowModifications);
} else {
$this->_data[$key] = $value;
$this->_count = count($this->_data);
Many developers prefer using arrays in php file to save config data, such as $option['path'] = ''. From this constructor, we know that to load the config data, we can instantiate a Zend_Config class in this way: $config = new Zend_Config(require 'config.php');

Zend_Config use getter to retrieve config data. It provide magic method __get as well. However, __get also call the getter.

public function get($name, $default = null)
$result = $default;
if (array_key_exists($name, $this->_data)) {
$result = $this->_data[$name];
return $result;

public function __get($name)
return $this->get($name);

So, to get the value of $option['path'], we can use either $config->get('path') or $config->path. I think most of PHP developers(including myself) cannot resist the temptation of using the latter one to get data.

That is pretty enough for Zend_Config class.

No comments: