Easy Application Configuration With patConfiguration - Speaking Native (
Page 7 of 11 )
If XML isn't really your
cup of tea, patConfiguration can also create configuration files using traditional
PHP as well - this can work better in some cases, since the configuration file
only needs to be include()d in your application, not parsed by an XML parser as
well. Consider the following example and its output, which demonstrate how this
works:
<?
// include class
require("patConfiguration.php");
// create patConfiguration
object
$conf = new patConfiguration;
// set config file locations
$conf->setConfigDir("config");
//
set config values
$conf->setConfigValue("screen.width", 500);
$conf->setConfigValue("screen.height",
650);
// write file
$conf->writeConfigFile("config.php", "php");
?>
Here's the output:
<?PHP
// Configuration generated by patConfiguration
$config = array();
$config["screen.height"]
= 650;
$config["screen.width"] = 500;
?>
As you can see, the variable value pairs defined in the patConfiguration table
get converted into a PHP associative array and written to a file. By default,
this associative array is named $config - you can change it by providing an alternative
name via the "varname" argument to writeConfigFile().
<?
// include class
require("patConfiguration.php");
// create patConfiguration
object
$conf = new patConfiguration;
// set config file locations
$conf->setConfigDir("config");
//
set config values
$conf->setConfigValue("screen.width", 500);
$conf->setConfigValue("screen.height",
650);
// write file
$conf->writeConfigFile("config.php", "php", array("varname"
=>
"appConfig"));
?>
Here's the revised output:
<?PHP
// Configuration generated by patConfiguration
$appConfig = array();
$appConfig["screen.height"]
= 650;
$appConfig["screen.width"] = 500;
?>