How to Specify Non-String PHP Data Types in Zend Config XML and INI Files
Zend Config INI and Zend Config XML are adapters in the Zend Framework that allow you to store your application or website configuration data (such as database connector information and salt values) in an easy to interpret text file (I happen to prefer INI files over XML because INI is more terse).
The biggest downside to these files is that they don’t store PHP data types or values like NULL, FALSE, or TRUE, and you can’t specify an int, double, or float, the adapters only return strings.
Here is the recursive code for converting all your values:
public static function convertConfigValuesFromString($config)
{
foreach($config as $key => $value)
{
if(is_string($value))
{
switch($value)
{
case 'BOOL_FALSE':
$config[$key] = false;
break;
case 'BOOL_TRUE':
$config[$key] = true;
break;
case 'CONST_NULL':
$config[$key] = null;
break;
default:
if(substr($value,0,4) == 'INT_')
{
$config[$key] = (int) substr($value,4);
}
elseif(substr($value,0,4) == "DBL_")
{
$config[$key] = (double) substr($value,4);
}
elseif(substr($value,0,4) == "FLT_")
{
$config[$key] = (float) substr($value,4);
}
break;
}
}
elseif(is_array($value))
{
$config[$key] = self::convertConfigValuesFromString($value);
}
}
return $config;
}
Here is how I use it in my Bootstrap.php file in my Zend Framework application:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected $_namespace = 'Ashurex';
protected $_config;
... other code ...
protected function _initConfig()
{
// Use the default mechanisms built into the bootlstrap
// for reading in the app configuration
$config = $this->getOptions();
$config = Ashurex_Utilities::convertConfigValuesFromString($config);
// After gathering and assigning the
// correct data types to our config values
// Turn into an instance of Zend_Config for later use
$config = new Zend_Config($config);
// Store the config in the registry
Zend_Registry::set('config', $config);
$this->_config = $config;
return $config;
}
... other code ...
}
The following config file will end up looking like the following array (before turning it into a Zend_Config object)
[default] phpSettings.date.timezone = "America/Los_Angeles" bootstrap.path = APPLICATION_PATH "/Bootstrap.php" resources.modules[] = resources.view.doctype = "HTML5" app.debug.enabled = "BOOL_TRUE" resources.db.adapter = PDO_MYSQL [development : default] resources.db.params.host = "localhost" resources.db.params.username = "dbuser" resources.db.params.password = "dbpassword" resources.db.params.dbname = "dbname" app.integer = "INT_1234567890" app.double = "DBL_123456789012345.678" app.float = "FLT_12.0000001" session.name = "session_name"
array
'phpSettings' =>
array
'date' =>
array
'timezone' => string 'America/Los_Angeles' (length=19)
'bootstrap' =>
array
'path' => string '/var/www/html/application/Bootstrap.php' (length=50)
'resources' =>
array
'modules' =>
array
0 => string '' (length=0)
'view' =>
array
'doctype' => string 'HTML5' (length=5)
'db' =>
array
'adapter' => string 'PDO_MYSQL' (length=9)
'params' =>
array
...
'app' =>
array
'debug' =>
array
'enabled' => boolean true
'integer' => int 1234567890
'double' => float 1.2345678901235E+14
'float' => float 12.0000001
'session' =>
array
'name' => string 'session_name' (length=12)