一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

php INI配置文件的解析實現(xiàn)分析

所以看到這篇文章的時候,我也才剛剛知道,原來,還有一個dba的函數(shù)可以用,嗯,仔細看了一下dba這個函數(shù)的installtion,發(fā)現(xiàn)支持inifile也是從php5才開始實現(xiàn)的。好吧,相應的dba相關(guān)的可以看看這里: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/。

曾經(jīng)介紹過SPL的各類型接口和迭代器。今天,在瀏覽php源碼目錄時,發(fā)現(xiàn)有個解析INI文件的例子,覺得不錯,于是整理了一個實例,拿來分享下。

php應用程序中,配置文件不可或缺,特別是商城,CMS之類的產(chǎn)品,不同的客戶需求不同,當然,不會每個客戶開發(fā)一套程序,好辦法的是每個客戶 有一套不同的配置文件。適合做配置文件的我曾經(jīng)也說過,主要有四類:php數(shù)組(幾乎其他的配置方法最終都是解析成為php數(shù)組),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接口,當然要實現(xiàn)里面的5個迭代方法。迭代方法對handlerhandlerINI文件的解析,用到了dba擴展。

說點題外話,什么是Dba?為什么使用Dba?
Dba是一款數(shù)據(jù)庫,確切點說,是一款索引化的文件存儲系統(tǒng)。適合相對比較靜態(tài)的索引化的數(shù)據(jù)存儲。所有版本的Linux都會帶此數(shù)據(jù)庫。
既然使用文件來存儲數(shù)據(jù),為什么還有使用Dba呢?原因有二:
1數(shù)據(jù)記錄的存儲長度可以不是固定的;
2使用索引存儲和檢索數(shù)據(jù)。

DbaReader提供一個訪問INI文件數(shù)據(jù)的迭代方法,如果需要存儲刪除數(shù)據(jù)呢?所以DbaArray在繼承DbaReader的基礎(chǔ)上,實現(xiàn)了此功能。
復制代碼 代碼如下:
<?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);
}
}
?>

使用范例
構(gòu)建文件text.ini,內(nèi)容如下:
復制代碼 代碼如下:
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,這真是一個永恒的痛。)

php技術(shù)php INI配置文件的解析實現(xiàn)分析,轉(zhuǎn)載需保留來源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 色综合久久91 | 美女黄区 | 色一色在线观看视频网站 | 激情小视频在线播放免费 | 伊人首页 | 最大胆极品欧美人体 | 国产成人精品免费视频网页大全 | 国产播放器一区 | 成人va视频| 四虎在线最新永久免费播放 | 久久国产成人 | 男女视频免费网站 | 91麻豆精品一二三区在线 | 国产第二区 | 八戒精品无人区1区2区3区 | 久久青草免费91线频观看不卡 | 亚洲成人一级片 | 免费看黄色录像片 | 五月婷婷在线视频 | 国产手机国产手机在线 | 美女综合网 | 一级囗交片 | 精品一区二区三区无卡乱码 | 91亚洲区国产区精品区 | 国产级a爱做片免费观看 | 4hu永久影院在线四虎 | 国产精品亚洲第一区焦香 | 中文字幕一区二区三区永久 | 国产精品手机在线亚洲 | 亚洲国产精品成人午夜在线观看 | 国产视频播放 | 狠狠色成人综合首页 | 日本欧美一区二区三区在线 | 色在线播放| 国产在线精品一区二区三区不卡 | 一二三四日本高清 | 国产小视频在线免费观看 | 精品国产香蕉在线播出 | 精品国产免费福利片 | 国产精品一区二区免费 | 国产精品高清视亚洲一区二区 |