• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

php 搞代码 3年前 (2022-01-26) 24次浏览 已收录 0个评论

所以看到这篇文章的时候,我也才刚刚知道,原来,还有一个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:

 <BR><?php <BR>class DbaReader implements Iterator <BR>{ <br><br>protected $db = NULL; <BR>private $key = false; <BR>private $val = false; <br><br>/** <BR>* Open database $file with $handler in read only mode. <BR>* <BR>* @param file Database file to open. <BR>* @param handler Handler to use for database access. <BR>*/ <BR>function __construct($file, $handler) { <BR>if (!$this->db = dba_open($file, 'r', $handler)) { <BR>throw new exception('Could not open file ' . $file); <BR>} <BR>} <br><br>/** <BR>* Close database. <BR>*/ <BR>function __destruct() { <BR>dba_close($this->db); <BR>} <br><br>/** <BR>* Rewind to first element. <BR>*/ <BR>function rewind() { <BR>$this->key = dba_firstkey($this->db); <BR>$this->fetch_data(); <BR>} <br><br>/** <BR>* Move to next element. <BR>* <BR>* @return void <BR>*/ <BR>function next() { <BR>$this->key = dba_nextkey($this->db); <BR>$this->fetch_data(); <BR>} <br><br>/** <BR>* Fetches the current data if $key is valid <BR>*/ <BR>private function fetch_data() { <BR>if ($this->key !== false) { <BR>$this->val = dba_fetch($this->key, $this->db); <BR>} <BR>} <br><br>/** <BR>* @return Current data. <BR>*/ <BR>function current() { <BR>return $this->val; <BR>} <br><br>/** <BR>* @return Whether more elements are available. <BR>*/ <BR>function valid() { <BR>if ($this->db && $this->key !== false) { <BR>return true; <BR>} else { <BR>return false; <BR>} <BR>} <br><br>/** <BR>* @return Current key. <BR>*/ <BR>function key() { <BR>return $this->key; <BR>} <BR>} <BR>?> <BR>


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

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

DbaReader提供一个访问INI文件数据的迭代方法,如果需要存储删除数据呢?所以DbaArray在继承DbaReader的基础上,实现了此功能。

 <BR><?php <BR>class DbaArray extends DbaReader implements ArrayAccess <BR>{ <br><br>/** <BR>* Open database $file with $handler in read only mode. <BR>* <BR>* @param file Database file to open. <BR>* @param handler Handler to use for database access.取值http://www.php.net/manual/en/dba.requirements.php <BR>*/ <BR>function __construct($file, $handler) <BR>{ <BR>$this->db = dba_popen($file, "c", $handler); <BR>if (!$this->db) { <BR>throw new exception("Databse could not be opened"); <BR>} <BR>} <br><br>/** <BR>* Close database. <BR>*/ <BR>function __destruct() <BR>{ <BR>parent<strong style="color:transparent">来2源gaodaima#com搞(代@码&网</strong><label>搞gaodaima代码</label>::__destruct(); <BR>} <br><br>/** <BR>* Read an entry. <BR>* <BR>* @param $name key to read from <BR>* @return value associated with $name <BR>*/ <BR>function offsetGet($name) <BR>{ <BR>$data = dba_fetch($name, $this->db); <BR>if($data) { <BR>if (ini_get('magic_quotes_runtime')) { <BR>$data = stripslashes($data); <BR>} <BR>//return unserialize($data); <BR>return $data; <BR>} <BR>else <BR>{ <BR>return NULL; <BR>} <BR>} <br><br>/** <BR>* Set an entry. <BR>* <BR>* @param $name key to write to <BR>* @param $value value to write <BR>*/ <BR>function offsetSet($name, $value) <BR>{ <BR>//dba_replace($name, serialize($value), $this->db); <BR>dba_replace($name, $value, $this->db); <BR>return $value; <BR>} <br><br>/** <BR>* @return whether key $name exists. <BR>*/ <BR>function offsetExists($name) <BR>{ <BR>return dba_exists($name, $this->db); <BR>} <br><br>/** <BR>* Delete a key/value pair. <BR>* <BR>* @param $name key to delete. <BR>*/ <BR>function offsetUnset($name) <BR>{ <BR>return dba_delete($name, $this->db); <BR>} <BR>} <BR>?> <BR>


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

 <BR>host = localhost <BR>password = password <BR>database = data <BR>


文件index.php.代码如下:

 <BR><?php <BR>function loadClass($class) <BR>{ <BR>require_once __DIR__.DIRECTORY_SEPARATOR.$class.'.php'; <BR>} <BR>spl_autoload_register('loadClass',false); <br><br>$iniFile = __DIR__.DIRECTORY_SEPARATOR.'test.ini'; <br><br>$ini = new DbaArray($iniFile,'iniFile'); <BR>echo $ini['database']; <BR>var_dump($ini); <BR>?> <BR>


–EOF–

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php INI配置文件的解析实现分析_php技巧

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址