[PHP]ConfigTool

Results 1 to 2 of 2
  1. #1
    Account Upgraded | Title Enabled! Exiled Hero is offline
    MemberRank
    Nov 2008 Join Date
    Multiple PlacesLocation
    202Posts

    [PHP]ConfigTool

    Got bored. D:

    This create configuration files and pulls values from it. It's actually good for those who want to use flat file as databases or store some settings in them, even if you use a database.

    Good for first timers. Coded in PHP4.

    Examples of class usage:

    PHP Code:
    <?php
    /**
     * Configuration tool examples
     */
    // Set notices off from error reporting if you want to use simpliest
    // and straight way to get configuration variable names.
    error_reportingE_ALL E_NOTICE );
    // In development environment you should have:
    // error_reporting( E_ALL );
    /**
    * include class and make object for the example
    */
    include( "../ConfigTool.php" );
    $conf = new ConfigTool();
    // to get configuration information from the text file
    // any relative path can be used BUT absolute path must be used
    // if you want to make changes and save new configuration files!
    // absolute path is made by custom getPath() function.
    $conf->setConfigFromFilegetPath() . "config.txt" );
    // set indent vallue. this is the number of characters between
    // start of key name and start of value
    $conf->setIndent15 );
    /************************************************************
    **                     EXAMPLE ONE
    ************************************************************/
    // show hello variables
    echo "CONFIG CONTENTS (config.txt): <br />";
    echo 
    "<br />hello1 = " $conf->get'hello1' );
    echo 
    "<br />hello2 = " $conf->get'hello2' );
    echo 
    "<br />hello3 = " $conf->get'hello3' );
    // edit name value pair
    $conf->updateKeyValue"hello1""Greetings!!!" );
    // delete name value pair
    $conf->deleteKey"hello2" );
    // add new name value pair
    $conf->addKeyValue"hello3""'Greetings from Norway!'" );
    /************************************************************
    **                      EXAMPLE TWO
    ************************************************************/
    // show again hello variables
    echo "<br /><br />CONFIG CONTENTS AFTER EDIT, IN MEMORY ONLY: <br />";
    echo 
    "<br />hello1 = " $conf->get'hello1' );
    echo 
    "<br />hello2 = " $conf->get'hello2' );
    echo 
    "<br />hello3 = " $conf->get'hello3' );
    // save modified object to another file
    $conf->setFileNamegetPath() . "my_conf.txt" );
    $conf->saveToFile();
    /************************************************************
    **                       EXAMPLE THREE
    ************************************************************/
    // make new object and get new configuration from file
    // we want to see, if above modified object was really saved...
    $conf2 = new ConfigTool();
    $conf2->setConfigFromFilegetPath() . "my_conf.txt" );
    // show hello variables
    echo "<br /><br />NEW CONFIG CONTENTS FROM FILE: <br />";
    echo 
    "<br />hello1 = " $conf2->get'hello1' );
    echo 
    "<br />hello2 = " $conf2->get'hello2' );
    echo 
    "<br />hello3 = " $conf2->get'hello3' );
    /************************************************************
    **                   GETPATH FUNCTION
    ************************************************************/
    /**
     * Additional function to get current script absolute path
     * @access public
     * @return string path
     */
    function getPath()
    {
        
    $path pathinfo$_SERVER['PHP_SELF'] );
        
    $path substr$_SERVER['DOCUMENT_ROOT'], 0, -) . $path['dirname'] . "/";
        return 
    $path;
    }

    /*EOF*/
    I threw in the config text file into the download, so you know what it looks like. I left comments in ConfigTool.php for thoughs who are willing to learn.
    Attached Files Attached Files
    Last edited by Exiled Hero; 28-04-10 at 12:18 PM.


  2. #2
    Software Engineer Evil[]Power is offline
    MemberRank
    Apr 2010 Join Date
    Look behind...Location
    1,191Posts

    Re: [PHP]ConfigTool

    Nice job thanks man ;)



Advertisement