我不知道怎么说才好,因为我在读INI文件的时候,往往都是用现成的函数:parse_ini_file或者是parse_ini_string,但怎么写入,就是另外的方法了(自己实现。。。
所以看到这篇文章的时候,我也才刚刚知道,原来,还有一个dba的函数可以用,嗯,香港虚拟主机,仔细看了一下dba这个函数的installtion,发现支持inifile也是从PHP5才开始实现的。好吧,虚拟主机,相应的dba相关的可以看看这里:,详细的还是看这里吧:
复制代码 代码如下:
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;
}
}
?>
复制代码 代码如下:
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.取值
*/
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);
}
}
?>
复制代码 代码如下:
host = localhost
password = password
database = data
复制代码 代码如下:
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);
?>