This article describes the Zend Framework tutorial Zend_Config_Ini usage with examples. Share it with everyone for your reference, the details are as follows:
Zend_Config_Ini allows developers to store and read configuration data in the application using the familiar INI format through nested object property 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 parse_ini_file()PHP function. Please review this documentation to understand 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 Splitter
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 which 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 configuration data is integrated from production site configuration data and can be overridden if needed
[staging : production] database.params.host = dev.example.com database.params.username = devuser database.params.password = devsecret
Next, assume that the developer needs to get development configuration data from the INI file. This is very simple, just specify the INI file and the 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"
Attention
Table Zend_Config_Ini constructor parameters:
<table class="jbborder" border="1" summary="Zend_Config_Ini 构造器参数"><thead><tr><th>参数</th><th>注释</th></tr></thead><tbody><tr><td><code class="code"><font color="#000000" face="NSimsun">$filename</font></code></td><td>要加载的 INI 文件。</td></tr><tr><td><code class="code"><font color="#000000" face="NSimsun">$section</font></code></td><td>在INI文件中 [section] (节)将被加载。把这个参数设置为null,所有的节将被加载。另外,一个节名称的数组被提供给加载多个节。</td></tr><tr><td><code class="code"><font color="#000000" face="NSimsun">$options = false</font></code></td><td>选项数组。下面的键被支持: <ul type="disc"><li><p style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px"><span class="emphasis"><em>allowModifications</em></span>:设置为true 允许随后加载文件更改。缺省为false</p></li><li><p style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px"><span class="emphasis"><em>nestSeparator</em></span>: 设置嵌套字符。缺省为"."</p></li></ul></td></tr></tbody></table>
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.