PHP读取配置文件类(php,ini,yaml,xml)

Php代码   收藏代码
  1. <?php  
  2. class Settings {  
  3.     var $_settings = array();  
  4.   
  5.     function get($var) {  
  6.         $var = explode('.'$var);  
  7.         $result = $this->_settings;  
  8.         foreach ($var as $key) {  
  9.             if (!isset ($result [$key])) {  
  10.                 return false;  
  11.             }  
  12.             $result = $result [$key];  
  13.         }  
  14.         return $result;  
  15.     }  
  16.   
  17.     function load() {  
  18.         trigger_error('Not yet implemented', E_USER_ERROR);  
  19.     }  
  20. }  
  21.   
  22. class Settings_PHP extends Settings {  
  23.     function load($file) {  
  24.         if (file_exists($file) == false) {  
  25.             return false;  
  26.         }  
  27.   
  28.         // Include file  
  29.         include ($file);  
  30.         unset ($file);  
  31.   
  32.         // Get declared variables  
  33.         $vars = get_defined_vars();  
  34.   
  35.         // Add to settings array  
  36.         foreach ($vars as $key => $val) {  
  37.             if ($key == 'this')  
  38.                 continue;  
  39.             $this->_settings [$key] = $val;  
  40.         }  
  41.   
  42.     }  
  43. }  
  44.   
  45. class Settings_INI extends Settings {  
  46.     function load($file) {  
  47.         if (file_exists($file) == false) {  
  48.             return false;  
  49.         }  
  50.         $this->_settings = parse_ini_file($file, true);  
  51.     }  
  52. }  
  53.   
  54. class Settings_YAML extends Settings {  
  55.     function load($file) {  
  56.         if (file_exists($file) == false) {  
  57.             return false;  
  58.         }  
  59.   
  60.         include ('spyc.php');  
  61.         $this->_settings = Spyc::YAMLLoad($file);  
  62.     }  
  63. }  
  64.   
  65. class Settings_XML extends Settings {  
  66.     function load($file) {  
  67.         if (file_exists($file) == false) {  
  68.             return false;  
  69.         }  
  70.   
  71.         include ('xmllib.php');  
  72.         $xml = file_get_contents($file);  
  73.         $data = XML_unserialize($xml);  
  74.   
  75.         $this->_settings = $data ['settings'];  
  76.     }  
  77. }  
  78.   
  79. ?>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值