Example for the Config class in CVS

From: Date: Fri, 03 Jan 2003 14:51:14 +0000
Subject: Example for the Config class in CVS
Groups: php.pear.dev php.pear.general 
Request: Send a blank email to pear-dev+get-12065@lists.php.net to get a copy of this message
Hi, As we've been talking about it in the current thread about getting an array from an xml file, here is an example on how to use the Config class with php arrays. <?php require_once('Config.php'); $file = '/path/to/conf.php'; /** conf.php should contain a php array like this: <?php $conf['storage']['driver'] = 'sql'; $conf['storage']['params']['phptype'] = 'mysql'; $conf['storage']['params']['hostspec'] = 'localhost'; $conf['storage']['params']['username'] = 'horde'; $conf['storage']['params']['password'] = 'hordepass'; $conf['storage']['params']['database'] = 120; $conf['menu']['apps'] = array('imp', 'turba'); $conf['database_dir'] = 'dbdir'; ?> */ // 1. We create a new config container for colors $colors =& new Config_Container('section', 'colors'); $colors->createComment('Color configuration'); $colors->createDirective('bgcolor', '#FFFFFF'); $colors->createDirective('rows', '#CCCCCC'); $colors->createDirective('lines', '#FF0000'); $colors->createBlank(); 2. We parse the config file using the phparray parser $conf = new Config('phparray', array('name' => 'conf')); $conf->parseConfig($file); 3. We add the $colors container $root =& $conf->getRoot(); $root->addItem($colors); 4. That's it. Now display echo '<pre>'.htmlspecialchars($root->toString('phparray')).'</pre>'; // you can also try this for an apache version... // echo '<pre>'.htmlspecialchars($root->toString('apache')).'</pre>'; echo '<pre>'; var_dump($root->toArray()); echo '</pre>'; ?> Regards, Bertrand Mansion Mamasam

« previous php.pear.dev (#12065) next »