PHP Classes
elePHPant
Icontem

File: advanced_example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Marko Tapio Manninen  >  Config Tool  >  advanced_example.php  >  Download  
File: advanced_example.php
Role: Example script
Content type: text/plain
Description: Example2 how to use the class
Class: Config Tool
Read and write configuration text files
Author: By
Last change: Proper error reporting, added new getPath() function, uses safer get() method to get configuration variables.
Date: 11 years ago
Size: 3,260 bytes
 

Contents

Class file image Download
<?php
/**
 * Configuration tool examples
 * @package ConfigTool
 */
// 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 advanced 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() . "example_config_file.txt" );
// set indent vallue. this is the number of characters between
// start of key name and start of value
$conf->setIndent( 15 );
/************************************************************
** PART ONE
************************************************************/
// show hello variables
echo "CONFIG CONTENTS (example_config_file.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!'" );
/************************************************************
** PART 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();
/************************************************************
** PART 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;
}
/************************************************************
** END OF ADVANCED EXAMPLE SCRIPT
************************************************************/
?>