Reputation: 1966
I have a config file that contains an array of settings/parameters. The problem I'm running into is accessing this array from within a class. My first guess was to do something like this.
Settings File:
$config = array (
'db' => array (
'host' => 'localhost',
'password' => 'password',
'port' => 3306,
'database' => 'my_db',
'user' => 'root'
),
'email' => array (
'username' => '[email protected]',
'smtp' => 'smtp.somewhere.com',
'ssl' => true,
'password' => 'my_password',
'port' => 587
)
// other stuff
);
The Confiuration class
class CoreConfig
{
static $confArray;
public static function read($name)
{
return self::$confArray[$name];
}
public static function write($name, $value)
{
self::$confArray[$name] = $value;
}
}
And use it like: CoreConfig::write('username', $config['email']['username']);
. Example using it in a class:
Usage
class SomeClass {
public function __construct() {}
public function getUserName() {
return CoreConfig::read('username');
}
}
Although this works, it feels like it's considered bad practice. Alternatively I can take the initial $config
and load it right away in my $confArray
instead "writing", which looks like an ever worse idea. Is there another approach to this?
Upvotes: 0
Views: 76
Reputation: 26
It sure feels a bit weird to hard code anything in an application, even if you're doing a configuration file. There are many methods for doing what you're trying to do, but the most important thing is to keep all "hard coded" values at the same place.
If you have access to any kind of SQL to do your work, I would recommend to put all configurations values in a data table. Doing this, you avoid having any hard coded values directly in the code, since they're in a database.
Config class
class CoreConfig
{
static $confArray = null;
public static function read($name)
{
if(self::$confArray == null)
self::loadConfig();
return self::$confArray[$name];
}
private static function loadConfig()
{
self::$confArray = DatabaseManager::fetchAll("SELECT * FROM config");
}
}
Database Manager class
class DatabaseManager
{
private static $instance = null;
private static $host = "YOUR HOST NAME HERE";
private static $dbName = "YOUR DATABASE NAME";
private static $dbUser = "YOUR DATABASE USER";
private static $dbPass = "YOUR DATABASE PASSWORD";
private function __construct() {}
private static function getDb()
{
if(is_null(self::$instance))
{
self::$instance = new PDO("mysql:host=" .self::$host. ";dbname=" .self::$dbName, self::$dbUser, self::$dbPass);
self::$instance->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
return self::$instance;
}
public static function fetchAll($SQL)
{
$return = null;
try
{
$return = self::getDb()->query($SQL)->fetchAll();
}
catch(Exception $e)
{
// If you want to display errors directly on the page
echo "SQL Query Error<br />";
echo "Request: $SQL<br />";
echo $e->getMessage();
}
return $return;
}
}
You may refer to PDO Documentation on php.net in order to add features to the Database manager. You will have to add more methods if you want to be able to write in the database using PHP.
If you have no database options, then you can use an XML or JSON configuration file and parse it into an array at the moment of reading its values.
Hope this helps!
Upvotes: 1