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_reporting( E_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->setConfigFromFile( getPath() . "config.txt" );
// set indent vallue. this is the number of characters between
// start of key name and start of value
$conf->setIndent( 15 );
/************************************************************
** 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->setFileName( getPath() . "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->setConfigFromFile( getPath() . "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, -1 ) . $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.