I'm working on a config class for PHP, where I can easily bring in config options. Any improvements/suggestions are appreciated.
Example usage:
echo Config::get('database.host');
// Returns: localhost
Example config file:
<?php
return array(
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'example_database'
);
The class:
<?php
class Config {
/**
* All of the items from the config file that is loaded
*
* @var array
*/
public static $items = array();
/**
* Loads the config file specified and sets $items to the array
*
* @param string $filepath
* @return void
*/
public static function load($filepath)
{
static::$items = include('config/' . $filepath . '.php');
}
/**
* Searches the $items array and returns the item
*
* @param string $item
* @return string
*/
public static function get($key = null)
{
$input = explode('.', $key);
$filepath = $input[0];
unset($input[0]);
$key = implode('.', $input);
static::load($filepath);
if ( ! empty($key))
{
return static::$items[$key];
}
return static::$items;
}
}
I should also add that this doesn't seem to work with multidimensional arrays - I'm working on this. If anyone has a solution, I'd appreciate it.