View the analysis and implementation of the parsing and implementation of the computer configuration software php INI configuration file

WBOY
Release: 2016-07-29 08:44:10
Original
1148 people have browsed it

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;
}
}
?>


DbaReader uses the Iterator interface, and of course it must implement the 5 iteration methods inside. The iterative method uses the dba extension to parse the handlerhandlerINI file.
Let’s digress a little bit, what is a Dba? Why use Dba?
Dba is a database, to be precise, it is an indexed file storage system. Suitable for relatively static indexed data storage. All versions of Linux will come with this database.
Since files are used to store data, why do we still use DBA? There are two reasons:
1 The storage length of data records may not be fixed;
2 Use indexes to store and retrieve data.
DbaReader provides an iterative method for accessing INI file data. What if you need to store deleted data? Therefore, DbaArray implements this function based on inheriting DbaReader.

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);
}
}
?>


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

复制代码 代码如下:


host = localhost
password = password
database = data


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

以上就介绍了查看电脑配置软件 php INI配置文件的解析实现分析,包括了查看电脑配置软件方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!