php读取四种配置文件

 

php读取四种配置文件

标签: phpfilexmlincludefunctionyaml  



































  分类:
 

<?php
/**
* 读取4中配置的表信息,现支持php.ini,xml.yaml
*/
class Settings{
var $_settings = array();
/**
    * 获取某些设置的值
    *
    * @param unknown_type $var
    * @return unknown
    */
       function get($var) {
         $var = explode('.', $var);

         $result = $this->_settings;
         foreach ($var as $key) {
                   if (!isset($result[$key])) { return false; }

                   $result = $result[$key];
         }

         return $result;


        // trigger_error ('Not yet implemented', E_USER_ERROR);//引发一个错误
       }

       function load() {
            trigger_error ('Not yet implemented', E_USER_ERROR);
       }


}
/**
* 针对PHP的配置,如有配置文件
* $file=
<?php
$db = array();

// Enter your database name here:
$db['name'] = 'test';

// Enter the hostname of your MySQL server:
$db['host'] = 'localhost';

?>


具体调用:
include ('settings.php'); //原始环境假设每个类为单独的一个类名.php文件

// Load settings (PHP)
$settings = new Settings_PHP;
$settings->load('config.php');

echo 'PHP: ' . $settings->get('db.host') . '';

*
*/
Class Settings_PHP Extends Settings {
function load ($file) {
         if (file_exists($file) == false) { return false; }

         // Include file
         include ($file);
unset($file);   //销毁指定变量
$vars = get_defined_vars(); //返回所有已定义变量的列表,数组,变量包括服务器等相关变量,
//通过foreach吧$file引入的变量给添加到$_settings这个成员数组中去.
foreach ($vars as $key => $val) {
         if ($key == 'this') continue;

         $this->_settings[$key] = $val;
}

}

 

}


//读取INI文件,主要用到parser_ini_file函数,该函数返回一个数组,如第二个参数为true时则返回多维数组/
/**
* ini例子:
* [db]
name = test
host = localhost
调用例子:
$settings = new Settings_INI;
$settings->load('config.ini'); 
echo 'INI: ' . $settings->get('db.host') . '';

*
*/
Class Settings_INI Extends Settings {
function load ($file) {
         if (file_exists($file) == false) { return false; }
         $this->_settings = parse_ini_file ($file, true);
}
}

//读取XML文件,需要用到XML_PARSER//
/**
* XML例子:
    <?xml version="1.0" encoding="UTF-8"?>
<settings>
         <db>
                   <name>test</name>
                   <host>localhost</host>
         </db>
</settings>
调用例子:
// Load settings (XML)
$settings = New Settings_XML;
$settings->load('config.xml');
echo 'XML: ' . $settings->get('db.host') . '';

*
*/
Class Settings_XML Extends Settings {
function load ($file) {
       if (file_exists($file) == false) { return false; }

       /**xmllib.php为PHP XML Library, version 1.2b,相关连接:http://keithdevens.com/software/phpxml
       xmllib.php主要特点是把一个数组转换成一个xml或吧xml转换成一个数组
       XML_unserialize:把一个xml给转换 成一个数组
       XML_serialize:把一个数组转换成一个xml
       自PHP5起,simpleXML就很不错,但还是不支持将xml转换成数组的功能,所以xmlLIB还是很不错的. 
       */
       include ('xmllib.php');  
       $xml = file_get_contents($file);
       $data = XML_unserialize($xml);
       $this->_settings = $data['settings'];
}

}
//读取YAML格式文件///
/**
使用YAML必须使用到SPYC这个库,相关链接在http://spyc.sourceforge.net/
YAML配置例子:
db:
   name: test
   host: localhost


*/
Class Settings_YAML Extends Settings {
function load ($file) {
       if (file_exists($file) == false) { return false; }

       include ('spyc.php');
       $this->_settings = Spyc::YAMLLoad($file);
}

}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值