PHP
  Home arrow PHP arrow Page 6 - Easy Application Configuration With patConfiguration
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

Easy Application Configuration With patConfiguration
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 1
    2003-01-29


    Table of Contents:
  • Easy Application Configuration With patConfiguration
  • Plug And Play
  • Your Friendly Neighbourhood Spiderman
  • Anatomy Class
  • Version Control
  • The Write Stuff
  • Speaking Native
  • Not Your Type
  • When Time Is Money, Recycle!
  • Cache Cow
  • Link Zone

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Easy Application Configuration With patConfiguration - The Write Stuff
    ( Page 6 of 11 )

    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
     

       

    PHP ARTICLES

    - Using Directory Iterators to Build Loader Ap...
    - Using the spl_autoload() Functions to Build ...
    - Working Out of the Object Context to Build L...
    - Using the _autoload() Magic Function to Buil...
    - The Destruct Magic Function in PHP 5
    - The Autoload Magic Function in PHP 5
    - Developing a Recursive Loading Class for Loa...
    - The Sleep and Wakeup Magic Functions in PHP 5
    - Using the Clone Magic Function in PHP 5
    - Including Files Recursively with Loader Appl...
    - The Call Magic Function in PHP 5
    - Designing a Captcha System with PHP and MySQL
    - Using Static Methods to Build Loader Apps in...
    - The Isset and Unset Magic Functions in PHP 5
    - Advanced PHP Form Input Validation to Check ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT