ZendFramework tutorial-Zend_Config_Ini Usage Analysis-php instance

Source: Internet
Author: User
This article mainly introduces the Zend_Config_Ini usage in the ZendFramework tutorial, and analyzes in detail the skills related to Zend_Config_Ini operations and configuration data, for more information about Zend_Config_Ini, see the examples in this article. We will share this with you for your reference. The details are as follows:

Zend_Config_Ini allows developers to store and read configuration data in the application in the familiar INI format through the nested Object Property syntax. The INI format has expertise in providing a hierarchical structure with configuration data keys and the inheritance capability between configuration data sections. Configure the data level structure to separate the key value by using a dot or a period. A section can be extended or inherited from another section by carrying a colon (:) and the name of the inherited configuration data section after the section name.

Parse_ini_file

Zend_Config_Ini uses the parse_ini_file () PHP function. Review this document to learn about its specific behavior. It is used in Zend_Config_Ini, such as true, false, yes, no, and null.

Key Separator

By default, the key separator character is a period (.). However, you can modify $ 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: Use Zend_Config_Ini

This example shows the basic usage of Zend_Config_Ini for loading configuration data from the INI file.

In this example, there are production system and staging system configuration data.

Because the development system configuration data is similar to the production system configuration data, the Development System Section inherits from the production system section.

In this case, the result is arbitrary and can be reversed, that is, the Production System Section inherits from the Development System Section, although this cannot be used in more complex situations.

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

Production Site configuration data

[production]webhost         = www.example.comdatabase.adapter     = pdo_mysqldatabase.params.host   = db.example.comdatabase.params.username = dbuserdatabase.params.password = secretdatabase.params.dbname  = dbname

The development site configuration data is integrated from the production site configuration data and can be rewritten as needed

[staging : production]database.params.host   = dev.example.comdatabase.params.username = devuserdatabase.params.password = devsecret

Next, assume that the developer needs to retrieve the development configuration data from the INI file. This is very simple. You only need to 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; // output "dev.example.com" echo $ config-> database-> params-> dbname; // output "dbname"

Note:

Table Zend_Config_Ini constructor parameters:

 
 
Parameters Note
$filename INI file to be loaded.
$section In the INI file, the [section] (section) will be loaded. Set this parameter to null and all sections will be loaded. In addition, an array of section names is provided to load multiple sections.
$options = false Option array. The following keys are supported:

  • AllowModifications: Set to true to allow subsequent file loading changes. The default value is false.

  • NestSeparator: Set nested characters. The default value is "."

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.