Home arrow PHP arrow Page 3 - Configuration Manipulation With PHP Config

Your Friendly Neighbourhood Hulk - PHP

Tired of writing (and rewriting) code to manage your application's configuration variables? Take a look at the PEAR Config class, a PHP toolkit designed specifically for manipulating configuration files and the data within them. This article demonstrates using the Config class to read and write configuration files in XML, PHP and INI formats, and use built-in methods to easily build Web-based application configuration modules.

TABLE OF CONTENTS:
  1. Configuration Manipulation With PHP Config
  2. Plug And Play
  3. Your Friendly Neighbourhood Hulk
  4. Different Strokes
  5. Array Of Hope
  6. Up A Tree
  7. Changing Things Around
  8. Giving Birth
  9. Link Zone
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 66
September 04, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

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: