四虎精品视频-四虎精品成人免费网站-四虎黄色网-四虎国产视频-国产免费91-国产蜜臀97一区二区三区

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

所以看到這篇文章的時(shí)候,我也才剛剛知道,原來(lái),還有一個(gè)dba的函數(shù)可以用,嗯,仔細(xì)看了一下dba這個(gè)函數(shù)的installtion,發(fā)現(xiàn)支持inifile也是從php5才開(kāi)始實(shí)現(xiàn)的。好吧,相應(yīng)的dba相關(guān)的可以看看這里:http://www.php.NET/manual/en/dba.installation.php,詳細(xì)的還是看這里吧:http://www.php.NET/manual/en/book.dba.php

OK,上原文,它來(lái)自于:http://www.cardii.NET/php-spl-parse-ini-file/。

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

php應(yīng)用程序中,配置文件不可或缺,特別是商城,CMS之類(lèi)的產(chǎn)品,不同的客戶需求不同,當(dāng)然,不會(huì)每個(gè)客戶開(kāi)發(fā)一套程序,好辦法的是每個(gè)客戶 有一套不同的配置文件。適合做配置文件的我曾經(jīng)也說(shuō)過(guò),主要有四類(lèi):php數(shù)組(幾乎其他的配置方法最終都是解析成為php數(shù)組),XML,YAML和 INI。今天只講INI文件。ZendFramework使用此配置。

下看個(gè)DbaReader類(lèi)。文件名為 DbaReader.php
復(fù)制代碼 代碼如下:
<?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接口,當(dāng)然要實(shí)現(xiàn)里面的5個(gè)迭代方法。迭代方法對(duì)handlerhandlerINI文件的解析,用到了dba擴(kuò)展。

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

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

文件index.php.代碼如下:
復(fù)制代碼 代碼如下:
<?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--

看完上面這一段,是不是有什么想法?原來(lái)ini的操作也是這么的方便?不過(guò),如果是純讀取的話,我還是比較推薦于parse_ini_file之類(lèi)的(突然間忘了,如果編碼不一樣怎么辦?ansi/utf-8,這真是一個(gè)永恒的痛。)

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

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

主站蜘蛛池模板: 《可爱的小鸟》阅读答案| 散文诗二首批注| 许多组织都有自己的价值标准和行为理念 | 河北电视台| 爱欲1990未删减版播放| 今天是你的生日合唱谱二声部| 日本变态网站| 抖音网页| 电影《醉猴》刘家良主演| 我是特种兵剧情介绍| 褚阳| 李泽锋演过的所有电视剧| 性监狱电影| 大珍珠演员表介绍| 瑜伽焰口全集 简体字| 希比·布拉奇克| 同桌的你电影免费观看| 《流感》高清在线观看| 公共安全教育第一课| 贾冰又出新的喜剧电影| 实力主义教室第三季| 勇者1—42集免费观看电视剧视频| 军官与男孩| 刷完牙嘴里有白色黏膜怎么回事 | 女子露胸| 飞天猪| 281封信电视剧演员表| 新红楼梦惊艳版| 养小动物的作文| 榜上有名| 王宝强 唐人街探案| 养小动物的作文| 珍珠传奇 电视剧| 奇妙的植物世界阅读短文答案| 李慧珍演的电影有哪些| 军营医生 (1976)| 羽毛球队名诙谐有趣的名字| 楼下的女邻居| 一线钟情 电视剧| 林黛玉进贾府人物形象分析 | 张韶涵个人简历|