Home >Backend Development >PHP Tutorial >View the analysis and implementation of the parsing and implementation of the computer configuration software php INI configuration file
So when I read this article, I just found out that there is another dba function that can be used. Well, I took a closer look at the installtion of the dba function and found that support for inifile was only implemented in PHP5. Well, you can look here for the corresponding DBA related information: http://www.php.net/manual/en/dba.installation.php. For more details, please look here: http://www.php.net/ manual/en/book.dba.php
OK, the original text, it comes from: http://www.cardii.net/php-spl-parse-ini-file/.
I have introduced various types of interfaces and iterators of SPL. Today, when browsing the PHP source code directory, I found an example of parsing INI files. I thought it was good, so I compiled an example and shared it.
In PHP applications, configuration files are indispensable, especially for products such as shopping malls and CMS. Different customers have different needs. Of course, each customer will not develop a set of programs. A good idea is to have one set for each customer. Different configuration files. I once said that there are four main categories suitable for making configuration files: PHP arrays (almost other configuration methods are ultimately parsed into PHP arrays), XML, YAML and INI. Today we will only talk about INI files. ZendFramework uses this configuration.
Let’s take a look at the DbaReader class. The file name is DbaReader.php:
Copy the code The code is as follows:
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;
}
}
?>
Copy the code The code is as follows:
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);
}
}
?>
复制代码 代码如下:
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);
?>
以上就介绍了查看电脑配置软件 php INI配置文件的解析实现分析,包括了查看电脑配置软件方面的内容,希望对PHP教程有兴趣的朋友有所帮助。