PHP ini configuration file parsing and implementation Analysis _php tutorial

Source: Internet
Author: User
Tags spl rewind
So when I saw this article, I just learned that there was a DBA function that could be used, well, a look at the installtion of the DBA, and found that supporting Inifile was also starting from PHP5. Well, the corresponding DBA-related can look here: http://www.php.net/manual/en/dba.installation.php, detailed or look here: http://www.php.net/manual/en/book.dba.php

OK, on the original, it comes from: http://www.cardii.net/php-spl-parse-ini-file/.

Various types of interfaces and iterators that have been introduced for SPL. Today, when browsing the PHP source directory, found that there is an example of parsing INI file, feel good, so organized an example, to share the next.

In the PHP application, the configuration file is indispensable, especially the mall, CMS and other products, different customer needs, of course, not every customer to develop a set of procedures, a good way is that each customer has a different configuration file. Suitable for configuration files I have also said that there are four main classes: PHP arrays (almost all of the other configuration methods are eventually parsed into PHP arrays), Xml,yaml and INI. Only the INI file is spoken today. Zendframework Use this configuration.

Take a look at the Dbareader class. File name is dbareader.php:
Copy CodeThe code is as follows:
Class Dbareader implements Iterator
{

protected $db = NULL;
Private $key = false;
Private $val = false;

/**
* Open database $file with $handler on 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 is 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, of course, to implement the inside of the 5 iterative methods. The iterative method parses the Handlerhandlerini file and uses the DBA extension.

Say humorous, what is a DBA? Why use a DBA?
The DBA is a database, to be exact point, an indexed file storage system. Suitable for relatively static, indexed data storage. All versions of Linux will take this database.
Since you use files to store data, why do you use DBAs? There are two reasons:
1 The storage length of the data record can not be fixed;
2 use an index to store and retrieve data.

Dbareader provides an iterative way to access INI file data, and if you need to store delete data? Therefore, Dbaarray has implemented this function on the basis of inheriting dbareader.
Copy CodeThe code is as follows:
Class Dbaarray extends Dbareader implements Arrayaccess
{

/**
* Open database $file with $handler on read only mode.
*
* @param file Database file to open.
* @param handler handler to use for database access. Value 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);
}
}
?>

Usage examples
Build the file Text.ini with the following:
Copy CodeThe code is as follows:
host = localhost
Password = password
Database = Data

File index.php. Code is as follows:
Copy CodeThe code is as follows:
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--

After reading the above paragraph, is there any idea? The operation of the original INI is also so convenient? However, if it is read-only, I would rather recommend parse_ini_file and so on (suddenly forget, if the code is not the same? Ansi/utf-8, this is really an eternal pain. )

http://www.bkjia.com/PHPjc/322682.html www.bkjia.com true http://www.bkjia.com/PHPjc/322682.html techarticle So when I saw this article, I just learned that, originally, there is a DBA's function can be used, well, a careful look at the DBA this function installtion, found that support Inifile also ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.