Днес нещо ме удари музата и за около час написах един сравнително лесен за използване клас. Целта му е да зарежда конфигурационни файлове и по този начин да се отървем от използването на include и require.
<?php
/*
* Simple loader for config files/Singleton/
*
* Developed by HD 2012
*/
class ConfigLoader {
/*
* current instance of the class
*/
private static $_instance=NULL;
public $_options=array();
//the constructor
private function __construct() {}
//singleton's could not be cloned
private function __clone() {}
/*
* check the instance of the class.
* $filepath-път до файла
* $type-вид на файла
*/
public static function checkInstance($filepath, $type)
{
//check the instance of the class
if(self::$_instance==NULL)
{
self::$_instance=new self($filepath, $type);
}
else
{
//return the current instance of the class
return self::$_instance;
}
}
/*
* Loading the configurations
*/
private function load($filepath, $type)
{
switch ($type)
{
case "ARRAY":
$this->options= include $filepath;
break;
case "INI":
$this->_options= parse_ini($filepath, true);
break;
case "JSON":
break;
case "XML":
//checking if the file exist
{
}
else
{
}
}
}
}
?>
Пример на това как се използва
$config = Config::getInstance(PATH TO FILE, FILE TYPE);
echo $config->ip;
echo $config->db['host'];
example array file
<?php
'host' => 'mysite.com',
'user' => 'root',
'pass' => 'blalalalallala'),
'ip' => '127.0.0.1',
Надявам се да е полезно.. Забележки ако имате, коментари или просто искате да ми теглите една, моля под темата
