Home arrow PHP arrow Page 6 - Easy Application Configuration With patConfiguration

The Write Stuff - PHP

Tired of handcrafting configuration file manipulation tools for your Web application? Save yourself some time with patConfiguration, a PHP class designed to assist developers with reading, writing and maintaining application configuration files.

TABLE OF CONTENTS:
  1. Easy Application Configuration With patConfiguration
  2. Plug And Play
  3. Your Friendly Neighbourhood Spiderman
  4. Anatomy Class
  5. Version Control
  6. The Write Stuff
  7. Speaking Native
  8. Not Your Type
  9. When Time Is Money, Recycle!
  10. Cache Cow
  11. Link Zone
By: Vikram Vaswani, (c) Melonfire
Rating: starstarstarstarstar / 1
January 29, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
So that takes care of reading configuration files - now how about writing them?

In the patConfiguration universe, creating a configuration file is a two-step process: first add one or more entries (variable-value pairs) to the configuration stack, and then write the stack to a file. patConfiguration comes with a setConfigValue() method that takes care of the first step, and a writeConfigFile() method that takes care of the second one. Consider the following example, which demonstrates:


<? // 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.xml", "xml"); ?>


The setConfigValue() method accepts two primary parameters - variable name and corresponding value - and a third optional parameter, which is the datatype of the variable being set (if this third value is omitted, patConfiguration will automatically determine the variable type). Valid type values include "string", "integer", "boolean", "float" and "array" - I'll be discussing these again a little further down.

Once the variable-value pairs have been created, the writeConfigFile() method is called to actually write the configuration data to a file. In addition to the file name, patConfiguration also allows you to specify the file format - "xml" or "php" - together with a couple of miscellaneous options that adjust the behaviour of the writeConfigFile() method.

Here's the output of the example above:


<?xml version="1.0" encoding="ISO-8859-1"?> <configuration> <path name="screen"><configValue name="height" type="int">650</configValue><configValue name="width" type="int">500</configValue> </path></configuration>


Having trouble reading it? Have patConfiguration indent it a little more neatly with the additional "mode" parameter:


<? // 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.xml", "xml", array("mode" => "pretty")); ?>


Here's the revised output:


<?xml version="1.0" encoding="ISO-8859-1"?> <configuration> <path name="screen"> <configValue name="height" type="int">650</configValue> <configValue name="width" type="int">500</configValue> </path> </configuration>


You can set multiple configuration values at a time via the setConfigValues() method, which accepts an array of variable-value pairs


<? // include class require("patConfiguration.php"); // create patConfiguration object $conf = new patConfiguration; // set config file locations $conf->setConfigDir("config"); // create array of configuration values $config = array( "screen.height" => 650, "screen.width" => 500 ); // add to configuration table $conf->setConfigValues($config); // write file $conf->writeConfigFile("a.xml", "xml"); ?>


and clear a value using the clearConfigValue() method.


<? // include class require("patConfiguration.php"); // create patConfiguration object $conf = new patConfiguration; // set and display value $conf->setConfigValue("name", "Superman"); echo $conf->getConfigValue("name"); // clear and check to see if value exists $conf->clearConfigValue("name"); echo $conf->getConfigValue("name"); ?>


Note that if no path is provided to clearConfigValue(), the entire configuration table is cleared.

 
 
>>> More PHP Articles          >>> More By Vikram Vaswani, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: