Home  >  Article  >  php教程  >  Zend Framework tutorial Zend_Config_Ini usage analysis

Zend Framework tutorial Zend_Config_Ini usage analysis

高洛峰
高洛峰Original
2016-12-26 15:13:471123browse

The example in this article describes the usage of Zend_Config_Ini in the Zend Framework tutorial. Share it with everyone for your reference, as follows:

Zend_Config_Ini allows developers to store and read configuration data in the application using the familiar INI format through nested object attribute syntax. The INI format has expertise in providing hierarchical structures with configuration data keys and inheritance capabilities between configuration data sections. Configure the data hierarchy by separating key values ​​with dots or periods (.). A section can extend or inherit from another section by following the section name with a colon (:) and the name of the section of configuration data to be inherited.

parse_ini_file

Zend_Config_Ini uses the parse_ini_file()PHP function. Please review this document to learn about the specific behavior it uses in Zend_Config_Ini, such as how special values ​​such as true, false, yes, no and null are handled.

Key Separator

By default, the key separator character is a period (.). However, this can be modified by modifying the $options key 'nestSeparator' when constructing the Zend_Config_Ini object. For example:

$options['nestSeparator'] = ':';
$config = new Zend_Config_Ini('/path/to/config.ini',
               'staging',
               $options);


Example: Using Zend_Config_Ini

This example illustrates the basic usage of Zend_Config_Ini that loads configuration data from an INI file.

In this example there is configuration data for the production system and the staging system.

Because the development system configuration data is similar to the production system configuration data, the development system sections inherit from the production system sections.

In this case, the decision is arbitrary and it can be done the other way around, i.e. the production system section inherits from the development system section, although this is not possible for more complex cases.

Next, assume that the following configuration data is contained in /path/to/config.ini:

Production site configuration data

[production]
webhost         = www.example.com
database.adapter     = pdo_mysql
database.params.host   = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname  = dbname

Development site The configuration data is integrated from the production site configuration data and can be overridden if necessary

[staging : production]
database.params.host   = dev.example.com
database.params.username = devuser
database.params.password = devsecret

Next, assume that the developer needs to get the development configuration data from the INI file. This is very simple, just specify the INI file and development system section to load the data:

$config = new Zend_Config_Ini('/path/to/config.ini', 'staging');
echo $config->database->params->host;  // 输出 "dev.example.com"
echo $config->database->params->dbname; // 输出 "dbname"

Note

Table Zend_Config_Ini constructor parameters:

1Zend_Config_Ini 构造器参数:

For more articles related to Zend Framework tutorial Zend_Config_Ini usage analysis, please pay attention to the PHP Chinese website!

Statement:
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