I am creating a system for a client that loads information from an XML file and then parses the information where needed. For this, I have got this class:
<?php
class Config
{
private static $init;
private static $link;
private static $db_hostname;
private static $db_username;
private static $db_password;
private static $db_database;
public static $ABSPATH;
public static $GWPATH;
public static $CONFIGPATH;
public static $CONNECTIONPATH;
public static $configfile;
public static $connectionfile;
private static $cfg;
const CONFIGFILEPATH = "E:/projects/php/_site/_site/_include/Config/web.config.xml";
/** Private to implement singleton pattern */
private function __construct() { }
/** @return Config */
public static function Init()
{
return static::$init = (
null === static::$init ? new self() : static::$init
);
}
public function RequireFiles()
{
require_once static::$connectionfile;
require_once static::$GWPATH . 'libraries/functions/user.loggedin.php';
}
public function LoadConfig()
{
static::$cfg = simplexml_load_file(self::CONFIGFILEPATH);
$path = static::$cfg->paths;
$database = static::$cfg->database;
static::$ABSPATH = (string) $path->abs;
static::$GWPATH = (string) $path->gwpath;
static::$CONFIGPATH = (string) $path->configpath;
static::$configfile = (string) $path->configfile;
static::$CONNECTIONPATH = (string) $path->connectionpath;
static::$connectionfile = (string) $path->connectionfile;
static::$db_hostname = (string) $database->hostname;
static::$db_username = (string) $database->username;
static::$db_password = (string) $database->password;
static::$db_database = (string) $database->name;
}
public function GetLink()
{
if (empty(static::$link) || null === static::$link)
$this->SetLink();
return static::$link;
}
public function SetLink()
{
static::$link =
new Database($this->GetDBHostname(), $this->GetDBUsername(), $this->GetDBPassword(), $this->GetDBDatabase())
or die("Failed to connect to the server. Error: " . static::$link->connect_error);
}
private function GetDBHostname()
{
return static::$db_hostname;
}
private function GetDBUsername()
{
return static::$db_username;
}
private function GetDBPassword()
{
return static::$db_password;
}
private function GetDBDatabase()
{
return static::$db_database;
}
public function GetConfigXML($do = false)
{
if ($do == true)
return static::$cfg;
return null;
}
}
This is working for me, but I wanted to know if there is a better way to do this?
Info:
This is used in this way:
$config = Config::Init();
$config->LoadConfig();
$config->RequireFiles();
$link = $config->GetLink();