Pragmatism in the real world

Zend_Config: Array Reader

When casting around for another file format for Zend Config, I came up with XML or YAML. Obviously I posted to the mailing list for thoughts and Paul Jones suggested the completely obvious: PHP arrays!

Another one, perhaps so obvious it is invisible, is a PHP array. Simple, straightforward, fast, no parser needed, open to programmatic manipulation. If it has not already been implemented, it would be trivial to do so.

And he’s right.

One of the nice things about the review process that the Zend_Config proposal went through is that the design is much better now. Essentially the loading of the file is separated from the accessing of config data. This means that writing new file loaders is that much simpler. In the case of Zend_Config_Array, it’s all done in the load() function and boils down to:
include $filename;
return $config[$section];

Obviously, this relies on the config file having an associative array called $config with a top level key called $section. For the unit tests, the following config file is used:

< ?php $config['all'] = array( 'hostname' => 'all',
'name' => 'thisname',
'db' => array(
'host' => '',
'user' => 'username',
'pass' => 'password',
'name' => 'live',
'one' => array(
'two' => array(
'three' => 'multi'

$config['staging'] = $config['all'];
$config['staging']['hostname'] = 'staging';
$config['staging']['db']['name'] = 'dbstaging';
$config['staging']['debug'] = false;

$config['other_staging'] = $config['staging'];
$config['other_staging']['only_in'] = 'otherStaging';
$config['other_staging']['db']['pass'] = 'anotherpwd';

// invalid section
$config['extendserror'] = 123;

As you can see, the “extends” keyword isn’t required in when using PHP arrays for config as we can assign directly from one array to another which makes life very simple.