PHP
  Home arrow PHP arrow Page 8 - 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 - Giving Birth
    ( Page 8 of 9 )

    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
    include("Config.php");

    // 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
    $c->setRoot($mailSection);

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

    ?>

    Here's the output:


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

    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
     

       

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