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

Anatomy Class - 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
Next, it's time to use the patConfiguration engine to read and use the values in the configuration file. Here's how:


<? // include class require("patConfiguration.php"); // create patConfiguration object $conf = new patConfiguration; // set config file locations $conf->setConfigDir("config"); // read config file $conf->parseConfigFile("config.xml"); // print configuration print_r($conf->getConfigValue()); ?>


Here's the output:


Array ( [application.name] => SomeApp [application.version] => 2.3 [application.window.height] => 600 [application.window.width] => 500 [application.window.list.maxItems] => 5 )


Let's dissect this a little to see how it works.

1. The first step is, obviously, to include all the relevant files for the class to work.


// include class require("patConfiguration.php");


Once that's done, I can safely create an object of the patConfiguration class.


// create patConfiguration object $conf = new patConfiguration;


This object instance will serve as the primary access point to the data in the application configuration file(s), allowing me to do all kinds of nifty things with it.

2. Next, the object's setConfigDir() method is used to set the default location of the configuration files,


// set config file locations $conf->setConfigDir("config");


and the parseConfigFile() method is used to actually read each file into the object's internal stack.


// read config file $conf->parseConfigFile("config.xml");


You can parse multiple configuration files by calling parseConfigFile() for each file, and telling patConfiguration to append (instead of overwriting) each set of configuration variables to the existing stack via the additional "a" option - as in the following code snippet:


// read config files $conf->parseConfigFile("config.main.xml"); $conf->parseConfigFile("config.local.xml", "a"); $conf->parseConfigFile("config.users.xml", "a");


4. Finally, all that's left is to actually use the configuration data - in this case, print it all to the standard output device.


// print configuration print_r($conf->getConfigValue());


The getConfigValue() method gets the value of a specified configuration variable from the configuration file(s). If no variable name is specified, the entire set of values is returned...as in the example above.

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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 4 - Follow our Sitemap

Dev Shed Tutorial Topics: