PHP
  Home arrow PHP arrow Page 4 - 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? 
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 - Different Strokes
    ( Page 4 of 9 )

    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
     

       

    PHP ARTICLES

    - 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 ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    Stay green...Green IT