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

<?php   

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

class Settings_PHP extends Settings {
	function load($file) {
		if (file_exists ( $file ) == false) {
			return false;
		}
		
		// Include file
		include ($file);
		unset ( $file );
		
		// Get declared variables
		$vars = get_defined_vars ();
		
		// Add to settings array
		foreach ( $vars as $key => $val ) {
			if ($key == 'this')
				continue;			
			$this->_settings [$key] = $val;
		}
	
	}
}

class Settings_INI extends Settings {
	function load($file) {
		if (file_exists ( $file ) == false) {
			return false;
		}
		$this->_settings = parse_ini_file ( $file, true );
	}
}

class Settings_YAML extends Settings {
	function load($file) {
		if (file_exists ( $file ) == false) {
			return false;
		}
		
		include ('spyc.php');
		$this->_settings = Spyc::YAMLLoad ( $file );
	}
}

class Settings_XML extends Settings {
	function load($file) {
		if (file_exists ( $file ) == false) {
			return false;
		}
		
		include ('xmllib.php');
		$xml = file_get_contents ( $file );
		$data = XML_unserialize ( $xml );
		
		$this->_settings = $data ['settings'];
	}
}
?> 

 php

/**  
  1. * 针对PHP的配置,如有配置文件  
  2. *config.php  
  3. <?php  
  4. $db = array();  
  5.  
  6. // Enter your database name here:  
  7. $db['name'] = 'test';  
  8.  
  9. // Enter the hostname of your MySQL server:  
  10. $db['host'] = 'localhost';  
  11. ?>  
  12.  
  13. //具体调用:  
  14. include ('settings.php'); //原始环境假设每个类为单独的一个类名.php文件  
  15. // Load settings (PHP)  
  16. $settings = new Settings_PHP;  
  17. $settings->load('config.php');  
  18. echo 'PHP: ' . $settings->get('db.host') . '';  
  19. *  
  20. */  
/**
* 针对PHP的配置,如有配置文件
*config.php
<?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') . '';
*
*/

 读取INI文件,主要用到parser_ini_file函数,该函数返回一个数组,如第二个参数为true时则返回多维数组

/**  
  1. * ini例子:config.ini  
  2.  
  3. [db]  
  4. name = test  
  5. host = localhost  
  6.  
  7. //调用例子:  
  8. $settings = new Settings_INI;  
  9. $settings->load('config.ini');  
  10. echo 'INI: ' . $settings->get('db.host') . '';  
  11. */  
/**
* ini例子:config.ini
* 
[db]
name = test
host = localhost

//调用例子:
$settings = new Settings_INI;
$settings->load('config.ini');
echo 'INI: ' . $settings->get('db.host') . '';
*/

 读取XML文件,需要用到XML_PARSER,xmllib.php在http://hudeyong926.iteye.com/admin/blogs/836048

/**  
  1. * XML例子:config.xml  
  2. <?xml version="1.0" encoding="UTF-8"?>  
  3. <settings>  
  4. <db>  
  5.     <name>test</name>  
  6.     <host>localhost</host>  
  7. </db>  
  8. </settings>  
  9.  
  10. // Load settings (XML)  
  11. $settings = New Settings_XML;  
  12. $settings->load('config.xml');  
  13. echo 'XML: ' . $settings->get('db.host') . '';  
  14. *  
  15. */  
/**
* XML例子:config.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') . '';
*
*/

 读取YAML格式文件,使用YAML必须使用到SPYC这个库,相关链接在http://spyc.sourceforge.net/

/**  
  1. YAML配置例子:config.yaml  
  2. db:  
  3.    name: test  
  4.    host: localhost   
  5.  
  6. // Load settings (YAML)  
  7. $settings = New Settings_YAML;  
  8. $settings->load('config.yaml');  
  9. echo 'YAML: ' . $settings->get('db.host') . '';  
  10. */  
/**
YAML配置例子:config.yaml
db:
   name: test
   host: localhost 

// Load settings (YAML)
$settings = New Settings_YAML;
$settings->load('config.yaml');
echo 'YAML: ' . $settings->get('db.host') . '';
*/

1。ini有点过时??

2。xml比较好,

3。yaml很好,但是毕竟没有标准化。

4。txt要自己组织格式,开放性不好。

5。类序列化。比较好,但是不熟悉的人使用比较麻烦!

6。php定义常量(你不用修改数据吗?)

所以:xml最好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值