PHP读取配置文件类实例(可读取ini,yaml,xml等)

yipeiwu_com6年前PHP代码库

本文实例讲述了PHP读取配置文件类实例。分享给大家供大家参考。具体如下:

<?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的配置,如有配置文件 
*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时则返回多维数组
/** 
* 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
/** 
* 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这个库
/** 
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最好。

希望本文所述对大家的php程序设计有所帮助。

相关文章

关于url地址传参数时字符串有回车造成页面脚本赋值失败的解决方法

在通过url地址接受参数的时候,有些参数的值V带有回车' %0A ',这时候在页面脚本显示的时候,把这个值V付给脚本变量,可能会造成脚本的错误。 所以,相应的:一开始在传值的时候对一些字...

PHP大批量数据操作时临时调整内存与执行时间的方法

复制代码 代码如下:ini_set('memory_limit', '250M'); //内存限制 set_time_limit(0); //...

解决php写入数据库乱码的问题

对于乱码这个问题php开发者几乎都会有碰到过,我们下面主要是介绍了php mysql数据库连接时乱码解决方法。 MYSQL数据库使用UTF-8编码的问题 1.用phpmyadmin创建...

php使用post数组的键值创建同名变量并赋值的方法

本文实例讲述了php使用post数组的键值创建同名变量并赋值的方法。分享给大家供大家参考。具体如下: 这段代码可以自动根据post数组的键值创建同名变量,这个功能使用非常方便,不用提前声...

php实现异步将远程链接上内容(图片或内容)写到本地的方法

本文实例讲述了php实现异步将远程链接上内容(图片或内容)写到本地的方法。分享给大家供大家参考,具体如下: /** * 异步将远程链接上的内容(图片或内容)写到本地 * * @...