PHP
  Home arrow PHP arrow Page 6 - Configuration Manipulation With PHP Co...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 5 stars5 stars5 stars5 stars5 stars / 58
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Configuration Manipulation With PHP Config - Up A Tree
    (Page 6 of 9 )

    In addition to the technique described previously, the Config class also includes a number of built-in methods to retrieve configuration values from the internal stack after they have been read in. In order to understand this alternative approach, you first need to conceptually understand how the Config class deals with a configuration file, and with the various sections within it.

    Seen through the eyes of Config, every configuration file can be broken down into four basic elements:

    1. Sections - blocks within the configuration file, each with a name, containing one or more directives.

    2. Directives - variable-value pairs

    3. Comments - explanatory notes which can be ignored, these are meant only for human consumption

    4. Blanks - blank lines, useful for cleaning up the visual appearance of a configuration file

    The root object returned by the parseConfig() method is a section, and as such, it exposes a number of different methods. For the moment, we'll focus on the getItem() method, used to return a reference to another section, to a directive, or to a comment. Once a reference is obtained to a directive or comment, other methods - the most common one is getContent() - become available to retrieve the content of that directive or comment.

    In order to better understand how this works, consider the following example, which does exactly what the previous example did, but uses object methods and properties to retrieve the various configuration values.


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

    // get reference to <mysql> element of file
    $mysqlSection =& $root->getItem("section", "mysql");

    // get reference to <host> element
    $hostDirective =& $mysqlSection->getItem("directive", "host"); // get content of <host> element $host = $hostDirective->getContent();

    // get reference to <user> element
    $userDirective =& $mysqlSection->getItem("directive", "user"); // get content of <user> element $user = $userDirective->getContent();

    // get reference to <pass> element
    $passDirective =& $mysqlSection->getItem("directive", "pass"); // get content of <pass> element $pass = $passDirective->getContent();

    // get reference to <db> element
    $dbDirective =& $mysqlSection->getItem("directive", "db");
    // get content of <db> element
    $db = $dbDirective->getContent();

    // 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);

    ?>

    If you've ever used the Document Object Model (DOM) in XML, the above probably looks strangely familiar to you. It should - the Config class uses a similar approach, providing methods to obtain references to different "nodes" of the configuration "tree", and thereby to retrieve the values of those nodes. Like the DOM, there are also methods to add, edit and delete nodes - but those come later.

    A quick note on the getItem() method above - it can be used to return a reference to any section of the configuration file and requires, as first argument, one of the keywords "section", "directive", "comment" or "blank" (to identify the type of data needed) and, as second argument, an identifier to help it locate the necessary node. A number of different options can be sent as additional arguments to getItem() to help it identify the correct node - take a look at the manual for detailed information on this.

    Once a reference to the correct node has been obtained, the getContent() method can be used to retrieve its value. This method may be used with both directives and comments.

    Here's another example, this time using a Windows INI file like the one below,


    [Settings]
    POPAccount=me@my.mail.domain
    SMTPServer=mail.my.domain.com
    Signature=default
    POPPassword=guessme

    [ToolBar-Summary]
    Bars=7
    ScreenCX=800
    ScreenCY=600

    and the following script to retrieve mail server access information:


    <?php

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

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

    // read configuration data and get reference to root
    $root =& $c->parseConfig("mail.ini", "IniFile");

    // get reference to [Settings] section of file
    $settingsSection =& $root->getItem("section", "Settings");

    // get POP account
    $popAccountDirective =& $settingsSection->getItem("directive", "POPAccount"); $popAccount = $popAccountDirective->getContent();

    // get password
    $passwordDirective =& $settingsSection->getItem("directive", "POPPassword"); $password = $passwordDirective->getContent();

    // print current configuration
    echo "System is currently configured for POP account $popAccount and password $password";

    ?>

    This follows much the same logic as before - the getItem() method is used to first obtain a reference to the [Settings] section of the INI file, and then to the appropriate directives within that section. Once the variable values have been retrieved via the getContent() method, they are printed to the output device.

    Here's the output:


    System is currently configured for POP account me@my.mail.domain and password guessme

    When retrieving configuration data in this manner, a number of utility methods are also available; these can come in handy in certain situations. Here's a quick list:

    getName() - retrieves the item name

    getType() - retrieves the item type

    getParent() - retrieves a reference to the item's parent

    getChild() - returns a reference to the item's child

    getItemPosition() - returns the item's position among its siblings

    countChildren() - returns a count of the item's children

    More PHP Articles
    More By icarus, (c) Melonfire


     

       

    PHP ARTICLES

    - Setting Up a Web-based Image Hosting Service
    - Comparing Files and Databases with PHP Bench...
    - Setting Up a Web-Based Image Gallery
    - Using Timers to Benchmark PHP Applications
    - Benchmarking Applications with PHP
    - Setting Up a Web-Based File Manager: PHPfile...
    - Developing a Modular Class For a PHP File Up...
    - Setting Up a Web-Based File Manager: bfExplo...
    - Defining a Custom Function for File Uploader...
    - Parsing Child Nodes with the DOM XML extensi...
    - Creating an Error Handling Module for a PHP ...
    - Accessing Attributes and Cloning Nodes with ...
    - Retrieving Information on Selected Files wit...
    - Handling HTML Strings and Files with the DOM...
    - Building File Uploaders with PHP 5

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway