"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,这真是一个永恒的痛。)