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

Different Strokes - 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

I noted, on the previous page, that altering the second argument to
writeConfig() significantly affects the format of the configuration file. Let's look at that a little more closely, by trying out the different variants. Consider the following rewrite of the previous example, which writes the configuration data as a generic configuration file instead of a PHP structure:


<?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");

// write configuration to file as generic conf file $c->writeConfig("mail.conf", "GenericConf");

?>

Here's what it looks like:


name:Bruce Banner
email:hulk@angry.green.guy
host:mail.apollo.domain

As with the PHPArray type, you can customize the output format by specifying the characters to be used for comments, delimiters and line endings. So, for example, if you wanted the variable-value pairs separated by commas, you could use this script,


<?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 delimiters
$options = array('equals' => ',');

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

?>

and the output would change to look like this:


name,Bruce Banner
email,hulk@angry.green.guy
host,mail.apollo.domain

You can write your configuration data in Windows INI file format with the following script,


<?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");

// write configuration to file as INI file $c->writeConfig("mail.conf", "IniFile");

?>

which produces this output:


name=Bruce Banner
email=hulk@angry.green.guy
host=mail.apollo.domain

And finally, you can even write configuration data using the XML standard, with the XML type (note that this requires the presence of two other XML classes from PEAR, the Parser class and the Util class in order to work). Here's the script,


<?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");

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

?>

and here's the output:


<?xml version="1.0" encoding="ISO-8859-1"?>
<name>Bruce Banner</name>
<email>hulk@angry.green.guy</email>
<host>mail.apollo.domain</host>

If you know a little bit about XML, you'll know that is not really well-formed XML, since the root element is missing. This can be easily rectified; just add an option specifying the name of this root element to the writeConfig(), as done earlier with the PHPArray type. Here's the revised script,


<?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 root element
$options = array('name' => 'mailSettings');

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

?>

and the revised, well-formed output:


<?xml version="1.0" encoding="ISO-8859-1"?>
<mailSettings>
<name>Bruce Banner</name>
<email>hulk@angry.green.guy</email>
<host>mail.apollo.domain</host>
</mailSettings>



 
 
>>> 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: