11.18.2005

Configuration variables via Singleton object in PHP4

There is probably no design pattern handier and easier to set up than a Singleton pattern. One of the best uses in PHP apps is for configuration variables. You can use define all over the place or you can use $GLOBAL but things work a lot better when you have nice neat place to set and get global configuration variables. Enter the Singleton Configuration object. Here's some simple code for the object itself, simply named Config.php:

/** 
* Web Config object using Singleton pattern 
* 
* @author Rich Zygler, Jr. 
* @datecreated 03/08/2005 
* @access public 
* @usage $config =& Config::getInstance(); 
* @usage $config->setConfig('admin_email', 'test@test.com'); 
* @usage $email = $config->getConfig('admin_email'); 
* @TODO 
*/ 

class Config 
{ 
  var $configVars = array(); 

/* 
* Create a new config object 
*/ 
  function &getInstance() 
  { 
    static $instance; 
    if (!$instance) 
    { 
      $instance = array(new Config); 
    } 
    return $instance[0]; 
  } 

/* 
* Get config data 
* 
* @param     $key string - array key of variable whose data we are retrieving 
* @return    $this->configVars string 
*/ 
  function getConfig($key) 
  { 
    return $this->configVars[$key]; 
  } 

/* 
* Set config data 
* 
* @param     $key string -variable to retrieve data stored 
* @param     $value string -variable that contains data to store 
* @return    true 
* Can use arrays as well as in  $config->setConfig('email_list', array(//... )); 
* retrieve array by foreach($config->getConfig('email_list') as $email) { //... } 
* 
*/ 

  function setConfig($key, $val) 
  { 
    $this->configVars[$key] = $val; 
    return true; 
  } 

} 


If you think the getInstance method looks a little borked, you're right. What we're doing there is attempting to return the first and only the first implementation of this object. So everytime you make a call to this config object, you should be getting the same one. That's why we return the [0]th item in the array. This is a little easier in PHP5 but for PHP4, this is what we're stuck with.

And here's how to use this bad boy:

/** 
* config include 
* 
* @author Rich Zygler, Jr. 
* @datecreated 03/08/2005 
* @TODO 
*/ 

// Bring in the Config class file 
require_once ('Config.php'); 

// Instantiate our config object 
// Remember in PHP4, we use the  =& 
// to pass by reference 

$config =& Config::getInstance(); 

// Set up some config vars 
// will change per application 
// The usage here is:     setConfig(varName, varValue) 

$config->setConfig('appVersion', '1.o'); 
$config->setConfig('appName','Tester'); 
$config->setConfig('debug', true); 

// Maybe set the DB connection vars 
$config->setConfig('dbconn', array( 
  'username' => 'bunsen', 
  'password' => '!honeydew%', 
  'host'     => 'localhost', 
  'db'       => 'test', 
  'type'     => 'mysql' 
  ) 
); 

// Now to get those values back from the $config object 

// first let's initialize our vars 
$appVersion = ''; 
$appName = ''; 
$debug = false; 
$dbConnArray = array(); // used for getting back db values 
$dbUser = ''; 
$dbPass = ''; 
$dbHost = ''; 
$dbType = ''; 
$dbName = ''; 

// Now let's get the values from the config object and populate our variables 
// of course, for a fully functioning app, you may want to check that these values are set 
$appVersion = $config->getConfig('appVersion'); 
$appName = $config->getConfig('appName'); 
$debug = $config->getConfig('debug'); 

// Here, we slurp the whole array from the config into a new array 
$dbConnArray = $config->getConfig('dbconn'); 
$dbUser  = $dbConnArray['username']; 
$dbPass  = $dbConnArray['password']; 
$dbHost     = $dbConnArray['host']; 
$dbType     = $dbConnArray['type']; 
$dbName    = $dbConnArray['db']; 

// Now let's echo everything out to make sure the values are 
// coming back correctly 
echo "The version is: $appVersion"; 
echo "The name is: $appName"; 
echo "The debugger is on: $debug (1 = true) "; 
echo "The db username is: $dbUser "; 
echo "The db password is: $dbPass "; 


What's great about this method of doing things is that you virtually eliminate the need for global variables in functions and objects. This is a very good thing. If you need some configuration variables available to a function or object, you can pass it this config object like this:

// call the function this way 
doSomethingWithConfig($config); 

// define the function this way 
// Remember to accept the object by reference (add the &) 
// otherwise a copy of the config data is created -- not good 

function doSomethingWithConfig(&$config) 
{ 
  // get app version 
  $appVersion = ''; 
  $appVersion = $config->getConfig('appVersion'); 
  return $appVersion; 
}


If you like this way of doing things, wait until you try to recreate this in PHP5, it's even easier.

No comments:

Post a Comment