PHP
  Home arrow PHP arrow Page 3 - Configuration Manipulation With PHP Config
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? 
Google.com  
PHP

Configuration Manipulation With PHP Config
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 61
    2003-09-04


    Table of Contents:
  • Configuration Manipulation With PHP Config
  • Plug And Play
  • Your Friendly Neighbourhood Hulk
  • Different Strokes
  • Array Of Hope
  • Up A Tree
  • Changing Things Around
  • Giving Birth
  • 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


    Configuration Manipulation With PHP Config - Your Friendly Neighbourhood Hulk
    ( Page 3 of 9 )

    Now that the hard sell is over and you're (hopefully) all set up with Config, let's take a simple example to see how it works.

    Let's assume that I have a PHP associative array containing a series of configuration variables, as in the following snippet:


    <?php
    $mailConfig = array(
    'name' => 'Bruce Banner',
    'email' => 'hulk@angry.green.guy',
    'host' => 'mail.apollo.domain'
    );
    ?>

    Normally, these configuration values would arrive via user input, perhaps via a form, perhaps through command-line entry. For simplicity and ease of understanding, however, I'm hard-wiring them for the moment; once you've understood the basics, a subsequent example will demonstrate how they can be extracted from a form.

    The task at hand now consists of storing them permanently, by saving them to a configuration file. With the Config class, this is a snap - consider the following script, which demonstrates:


    <?php

    // configuration data
    // hard-wired right now
    // usually taken from form input
    $mailConfig = array(
    'name' => 'Bruce Banner',
    'email' => 'hulk@angry.green.guy',
    'host' => 'mail.apollo.domain'
    );

    // include class
    include("Config.php");

    // instantiate object
    $c = new Config();

    // read configuration data and get reference to root
    $root =& $c->parseConfig($mailConfig, "PHPArray");

    // write configuration to file as PHP array $c->writeConfig("mail.conf", "PHPArray");

    ?>

    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
    include("Config.php");

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


    // instantiate object
    $c = new Config();

    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 parseConfig() method is used to read the configuration data into the object.


    // read configuration data and get reference to root
    $root =& $c->parseConfig($mailConfig, "PHPArray");

    An interesting point to note is the second argument to parseConfig(); this is a predefined type that tells the Config object the format or structure in which the configuration variables are stored, thereby simplifying the task of extracting those variables and restructuring them as per the object's internal requirements. The Config object defines a number of such formats, covering most of the major types of configuration files - here's a list of the important ones (there are a couple more, take a look at the manual for more details):

    1. "GenericConf" - a generic configuration file consisting of colon-delimited variable-value pairs, such as the following:


    mouse:Mickey
    duck:Donald

    2. "IniFile" - a Windows-style initialization (INI) file, such as the
    following:


    [characters]
    mouse=Mickey
    duck=Donald

    3. "PHPArray" - a PHP associative array, such as the following:


    <?php
    $mouse = 'Mickey';
    $duck = 'Donald';
    ?>

    4. "XML" - an XML file, such as the following:


    <?xml version="1.0" encoding="ISO-8859-1"?>
    <characters>
    <mouse>Mickey</mouse>
    <duck>Donald</duck>
    </characters>

    The second argument passed to parseConfig() thus tells the Config object how the configuration variables are stored, thereby activating the rulesets needed to read and manipulate them.

    4. Once the configuration data has been read into the object via parseConfig(), you can use it for whatever purpose you wish - to retrieve an individual value (or set of values) from it, to modify values, or to write the data to a file. Coincidentally, the script above picks door number three, writing the configuration data to a file named "mail.conf" in the current directory via the writeConfig() method.


    // write configuration to file as PHP array $c->writeConfig("mail.conf", "PHPArray");

    Note that here too, the second argument to writeConfig() is a type identifier - this tells Config what format the data should be written in. As you will see shortly, altering this parameter significantly affects the format of the configuration file.

    When you run this script via your Web browser, the configuration data in the PHP array $mailConfig will be written to a configuration file named "mail.conf", as noted previously. If you look into this file, you should see something like this:


    <?php
    $name = 'Bruce Banner';
    $email = 'hulk@angry.green.guy';
    $host = 'mail.apollo.domain';
    ?>

    If you don't like this, the writeConfig() method also offers you a further degree of customization when dealing with the PHPArray type - you can specify that the variable-value pairs be structured as an associative array of key-value pairs (rather than regular variables, as above) by adding an optional third argument to writeConfig(). Here's how:


    <?php

    // configuration data
    $mailConfig = array(
    'name' => 'Bruce Banner',
    'email' => 'hulk@angry.green.guy',
    'host' => 'mail.apollo.domain'
    );

    // include class
    include("Config.php");

    // instantiate object
    $c = new Config();

    // read configuration data and get reference to root
    $root =& $c->parseConfig($mailConfig, "PHPArray");

    // set options for output array
    $options = array('name' => 'mailSettings');

    // write configuration to file as PHP array $c->writeConfig("mail.conf", "PHPArray", $options);

    ?>

    In this case, the data written to "mail.conf" will be structured as an associative array with the name $mailSettings. Here's what the file would look like:


    <?php
    $mailSettings['name'] = 'Bruce Banner';
    $mailSettings['email'] = 'hulk@angry.green.guy'; $mailSettings['host'] = 'mail.apollo.domain'; ?>



     
     
    >>> More PHP Articles          >>> More By icarus, (c) Melonfire
     

       

    PHP ARTICLES

    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek