Analytic implementation of PHP INI configuration file _php Tips

Source: Internet
Author: User
Tags dba spl rewind
So when I see this article, I just know that, there is a DBA function can be used, ah, a careful look at the DBA this function of the installtion, found that support Inifile is also from the PHP5 to begin to realize. Well, the corresponding DBA can take a look here: http://www.php.net/manual/en/dba.installation.php, in detail 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 have been introduced for SPL. Today, when browsing the PHP source directory, found that there is a parse INI file example, feel good, and then sorted out an example, to share.

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

Next look at a Dbareader class. File name is dbareader.php:
Copy Code code as follows:

<?php
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-A.
*/
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, of course, to implement the 5 iterative methods inside. The iterative method resolves the Handlerhandlerini file and uses the DBA extension.

Say Han, what is a DBA? Why use DBA?
The DBA is a database and, more specifically, an indexed file storage system. Suitable for relatively static, indexed data storage. All versions of Linux will bring this database.
Why use a DBA if you use a file to store data? There are two reasons:
1 The storage length of the data record can not be fixed;
2 use indexes to store and retrieve data.

Dbareader provides an iterative method for accessing the INI file data, if you need to store the deleted data? So dbaarray on the basis of inheriting dbareader, this function is realized.
Copy Code code as follows:

<?php
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. 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 file Text.ini, which reads as follows:
Copy Code code as follows:

host = localhost
Password = password
Database = Data

File index.php. Code is as follows:
Copy Code code as follows:

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

Do you have any ideas for reading the above paragraph? The original INI operation is also so convenient? However, if it is pure read, I still prefer to recommend the Parse_ini_file and so on (suddenly forget, if the code is not the same how to do?) Ansi/utf-8, this is an eternity of pain. )

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.