Easy Application Configuration With patConfiguration - Not Your Type
(Page 8 of 11 )
You'll remember, from a couple pages back, that patConfiguration supports a number of different data types for its configuration variables. This variable type can be set via the setConfigValue() method, and currently can be any one of "string", "integer", "boolean", "array" and "float".
Here's an example which demonstrates how strings, integers and Booleans work:
<?
// 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, "integer");
$conf->setConfigValue("font.face",
"Verdana", "string");
$conf->setConfigValue("window.toolbar.visibility", true,
"boolean");
// write file
$conf->writeConfigFile("config.xml", "xml", array("mode"
=> "pretty"));
?>
Here's the XML output:
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<path name="font">
<configValue
name="face"
type="string">Verdana</configValue>
</path>
<path name="screen">
<configValue
name="width" type="int">500</configValue>
</path>
<path name="window">
<path
name="toolbar">
<configValue name="visibility"
type="bool">true</configValue>
</path>
</path>
</configuration>
Wanna use arrays in your configuration? patConfiguration supports those as well:
<?
// include class
require("patConfiguration.php");
// create patConfiguration
object
$conf = new patConfiguration;
// set config file locations
$conf->setConfigDir("config");
//
set config values
$conf->setConfigValue("friends", array("Rachel", "Ross", "Monica",
"Joey",
"Chandler", "Phoebe"), "array");
// write file
$conf->writeConfigFile("config.xml",
"xml", array("mode" => "pretty"));
?>
Here's the XML output,
<?xml version="1.0" encoding="ISO-8859-1"?>
<configuration>
<configValue
name="friends" type="array">
<configValue type="string">Rachel</configValue>
<configValue
type="string">Ross</configValue>
<configValue type="string">Monica</configValue>
<configValue
type="string">Joey</configValue>
<configValue type="string">Chandler</configValue>
<configValue
type="string">Phoebe</configValue>
</configValue>
</configuration>
and here's the PHP output:
<?PHP
// Configuration generated by patConfiguration
$config = array();
$config["friends"]
= array();
$config["friends"][0] = "Rachel";
$config["friends"][1] = "Ross";
$config["friends"][2]
= "Monica";
$config["friends"][3] = "Joey";
$config["friends"][4] = "Chandler";
$config["friends"][5]
= "Phoebe";
?>
Next: When Time Is Money, Recycle! >>
More PHP Articles
More By Vikram Vaswani, (c) Melonfire