当前位置: 移动技术网 > IT编程>开发语言>PHP > php INI配置文件的解析实现分析

php INI配置文件的解析实现分析

2019年04月24日  | 移动技术网IT编程  | 我要评论
所以看到这篇文章的时候,我也才刚刚知道,原来,还有一个dba的函数可以用,嗯,仔细看了一下dba这个函数的installtion,发现支持inifile也是从php5才开始实现的。好吧,相应的dba相关的可以看看这里:http://www.php.net/manual/en/dba.installation.php,详细的还是看这里吧:http://www.php.net/manual/en/book.dba.php

ok,上原文,它来自于:http://www.cardii.net/php-spl-parse-ini-file/。

曾经介绍过spl的各类型接口和迭代器。今天,在浏览php源码目录时,发现有个解析ini文件的例子,觉得不错,于是整理了一个实例,拿来分享下。

在php应用程序中,配置文件不可或缺,特别是商城,cms之类的产品,不同的客户需求不同,当然,不会每个客户开发一套程序,好办法的是每个客户 有一套不同的配置文件。适合做配置文件的我曾经也说过,主要有四类:php数组(几乎其他的配置方法最终都是解析成为php数组),xml,yaml和 ini。今天只讲ini文件。zendframework使用此配置。

下看个dbareader类。文件名为 dbareader.php:
复制代码 代码如下:

<?php
class dbareader implements iterator
{

protected $db = null;
private $key = false;
private $val = false;

/**
* open database $file with $handler in read only mode.
*
* @param file database file to open.
* @param handler handler to use for database access.
*/
function __construct($file, $handler) {
if (!$this->db = dba_open($file, 'r', $handler)) {
throw new exception('could not open file ' . $file);
}
}

/**
* close database.
*/
function __destruct() {
dba_close($this->db);
}

/**
* rewind to first element.
*/
function rewind() {
$this->key = dba_firstkey($this->db);
$this->fetch_data();
}

/**
* move to next element.
*
* @return void
*/
function next() {
$this->key = dba_nextkey($this->db);
$this->fetch_data();
}

/**
* fetches the current data if $key is valid
*/
private function fetch_data() {
if ($this->key !== false) {
$this->val = dba_fetch($this->key, $this->db);
}
}

/**
* @return current data.
*/
function current() {
return $this->val;
}

/**
* @return whether more elements are available.
*/
function valid() {
if ($this->db && $this->key !== false) {
return true;
} else {
return false;
}
}

/**
* @return current key.
*/
function key() {
return $this->key;
}
}
?>

dbareader使用iterator接口,当然要实现里面的5个迭代方法。迭代方法对handlerhandlerini文件的解析,用到了dba扩展。

说点题外话,什么是dba?为什么使用dba?
dba是一款数据库,确切点说,是一款索引化的文件存储系统。适合相对比较静态的索引化的数据存储。所有版本的linux都会带此数据库。
既然使用文件来存储数据,为什么还有使用dba呢?原因有二:
1数据记录的存储长度可以不是固定的;
2使用索引存储和检索数据。

dbareader提供一个访问ini文件数据的迭代方法,如果需要存储删除数据呢?所以dbaarray在继承dbareader的基础上,实现了此功能。
复制代码 代码如下:

<?php
class dbaarray extends dbareader implements arrayaccess
{

/**
* open database $file with $handler in read only mode.
*
* @param file database file to open.
* @param handler handler to use for database access.取值http://www.php.net/manual/en/dba.requirements.php
*/
function __construct($file, $handler)
{
$this->db = dba_popen($file, "c", $handler);
if (!$this->db) {
throw new exception("databse could not be opened");
}
}

/**
* close database.
*/
function __destruct()
{
parent::__destruct();
}

/**
* read an entry.
*
* @param $name key to read from
* @return value associated with $name
*/
function offsetget($name)
{
$data = dba_fetch($name, $this->db);
if($data) {
if (ini_get('magic_quotes_runtime')) {
$data = stripslashes($data);
}
//return unserialize($data);
return $data;
}
else
{
return null;
}
}

/**
* set an entry.
*
* @param $name key to write to
* @param $value value to write
*/
function offsetset($name, $value)
{
//dba_replace($name, serialize($value), $this->db);
dba_replace($name, $value, $this->db);
return $value;
}

/**
* @return whether key $name exists.
*/
function offsetexists($name)
{
return dba_exists($name, $this->db);
}

/**
* delete a key/value pair.
*
* @param $name key to delete.
*/
function offsetunset($name)
{
return dba_delete($name, $this->db);
}
}
?>

使用范例
构建文件text.ini,内容如下:
复制代码 代码如下:

host = localhost
password = password
database = data

文件index.php.代码如下:
复制代码 代码如下:

<?php
function loadclass($class)
{
require_once __dir__.directory_separator.$class.'.php';
}
spl_autoload_register('loadclass',false);

$inifile = __dir__.directory_separator.'test.ini';

$ini = new dbaarray($inifile,'inifile');
echo $ini['database'];
var_dump($ini);
?>

--eof--

看完上面这一段,是不是有什么想法?原来ini的操作也是这么的方便?不过,如果是纯读取的话,我还是比较推荐于parse_ini_file之类的(突然间忘了,如果编码不一样怎么办?ansi/utf-8,这真是一个永恒的痛。)

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网