PHP
  Home arrow PHP arrow Page 6 - Easy Application Configuration With pa...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
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: 5 stars5 stars5 stars5 stars5 stars / 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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    - Sub Classing Exceptions in PHP 5
    - Authentication for Web Application Security
    - Building a Content Management System with Co...
    - Filters and Login Systems for Web Applicatio...
    - Working with the Email Class in Code Igniter
    - Building Your Own System Tray Application Us...
    - Structuring Your Projects for Web Applicatio...
    - Inserting, Updating and Deleting Database Ro...
    - Building Your Own Desktop Notepad Applicatio...
    - Web Application Security Overview
    - Working with the Active Record Class in Code...
    - Generate PDF Documents with PHP on the Windo...
    - Sending Email with PHP Networking
    - Performing Strict Validation with the Code I...
    - The preg_replace_callback() function in PHP





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