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

Giving Birth - 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.

  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



The sharp-eyed amongst you would have noticed that the previous example made one important assumption: that the configuration file existed in the first place. If it didn't, the code on the previous page would return an error - not something you want happening the first time your application is installed.

With this in mind, the Config class also comes with a number of methods that allow you to create a complete configuration file from scratch. Consider the following example, which demonstrates:


// include file

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

// create section
$mailSection =& new Config_Container("section", "mail");

// create variables/values $mailSection->createDirective("name", "Bruce Banner"); $mailSection->createDirective("email", "hulk@angry.green.guy"); $mailSection->createDirective("host", "mail.apollo.domain");

// reassign root

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


Here's the output:

name=Bruce Banner

In this case, an object of the Config_Container class is initialized as a section named "mail", and a reference is obtained to it. This reference is used to build the rest of the configuration tree via the createDirective() methods. Once the tree is complete, a call to setRoot() takes care of resetting the root reference to the newly-created section, and a call to
writeConfig() writes the entire tree to the named file.

In addition to the createDirective() method, the class also includes createSection(), createComment() and createBlank() methods, designed specifically to create sections, comments and blank lines respectively. Take a look at the manual pages for more on these methods, together with usage examples.

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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