Examples of PHP reading four configuration files (php, ini, yaml, xml). The code is as follows. Take a look at it for yourself. You may be able to use it one day.
-
-
/** - * Read the table information configured in 4, now supports php.ini, xml.yaml
- */
- class Settings{
- var $_settings = array();
- /**
- * Get the value of some settings
- *
- * @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);
- }
- }
/**
- * For PHP configuration, if there is a configuration file
- * $file=
-
- $db = array();
// Enter your database name here:
- $db ['name'] = 'test';
// Enter the hostname of your MySQL server:
- $db['host'] = 'localhost';
- < ;p>?>
Specific call:
- include ('settings.php'); //The original environment assumes that each class is a separate class name .php file
// 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 example:
- * [db]
- name = test
- host = localhost
- Call example:
- $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例子:
-
-
- test
- localhost
-
- 调用例子:
- // 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 is PHP XML Library, version 1.2b, related link: http://keithdevens.com/software/phpxml
- The main feature of xmllib.php is to convert an array into an xml or convert xml into an array
- XML_unserialize: Convert an xml into an array
- XML_serialize: Convert an array into an xml
- Since PHP5, simpleXML is very good, but it still does not support the function of converting xml into an array, so xmlLIB is still very good.
- */
- include ('xmllib.php');
- $xml = file_get_contents($file);
- $data = XML_unserialize($xml);
- $this->_settings = $data['settings'];
- }
- }
- ////////////////////////////////////Read YAML format files////////// //////////////////////////////////////
- /**
- To use YAML, you must use the SPYC library. The relevant link is at http://spyc.sourceforge.net/
- YAML configuration example:
- 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);
- }
- }
-
Copy code
|