PHP
  Home arrow PHP arrow Page 5 - 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 - Array Of Hope
    ( Page 5 of 9 )

    So that takes care of writing configuration data to a file - now, how about reading it in?

    Reading configuration data in from a file is accomplished via the
    parseConfig() method discussed previously - the only difference is that where earlier you handed the method a PHP data structure, here you pass it a file name (together with the second argument indicating the type of file). Once the data has been read in, a number of other methods become available to access the various key-value pairs.

    Consider the following sample XML configuration file,


    <?xml version="1.0" encoding="UTF-8"?>
    <mysql>
    <host>localhost</host>
    <user>joe</user>
    <pass>secret</pass>
    <db>db456</db>
    </mysql>

    and this next PHP script, which takes care of reading it and converting the data within it to a native PHP structure:


    <?php

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

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

    // read configuration data and get reference to root
    $root =& $c->parseConfig("mysql.conf.xml", "XML");

    // convert data into PHP array
    print_r($root->toArray());

    ?>

    Here's the output:


    Array
    (
    [root] => Array
    (
    [mysql] => Array
    (
    [host] => localhost
    [user] => joe
    [pass] => secret
    [db] => db456
    )

    )

    )

    What happens here is pretty simple - the parseConfig() method reads the XML configuration file, parses it and places it into a private member of the object, and simultaneously exposes a root object that can be used to interact with the configuration variables. This root object comes with a
    toArray() method, which can be used to convert the XML configuration data into a native PHP associative array (there's also a toString() method that can be used to convert the data structure into a neatly-formatted string for display).

    If you want the value of a specific configuration variable (rather than the whole caboodle), it is simple to obtain it from the associative array created via toArray(),by drilling down to the appropriate key. Consider the following revision of the example above, which illustrates by retrieving the various configuration values and using them to connect to a MySQL
    database:


    <?php

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

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

    // read configuration data and get reference to root
    $root =& $c->parseConfig("mysql.conf.xml", "XML");

    // convert data into PHP array
    $config = $root->toArray();

    // extract configuration data
    $user = $config['root']['mysql']['user'];
    $pass = $config['root']['mysql']['pass'];
    $host = $config['root']['mysql']['host'];
    $db = $config['root']['mysql']['db'];

    // open connection to database
    $connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");
    mysql_select_db($db) or die ("Unable to select database!");

    // execute query
    $query = "INSERT INTO stocks (symbol, price) VALUES ('HYYJ', 198.78)"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error());

    // close database connection
    mysql_close($connection);

    ?>

    OOP purists will be pleased to hear that there's also a more elegant method to retrieve data from a configuration file - more on that on the next page.



     
     
    >>> 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 5 Hosted by Hostway
    Stay green...Green IT