Application Analysis _ php instance based on Zend Config mechanism

Source: Internet
Author: User
This article introduces the application analysis of the Zend-based Config mechanism. For more information, see The configuration class of Zend is in Zend_Config_Ini

Code
$ Config = new Zend_Config_Ini ("/var/www/html/usvn/config. ini", "general ");

Date_default_timezone_set ($ config-> timezone );

USVN_ConsoleUtils: setLocale ($ config-> system-> locale );

===

Config. ini file content

[General]

Url. base = "/usvn"

Translation. locale = "zh_CN"

Timezone = "Asia/Shanghai"


Detailed analysis
Here, only the constructor Zend_Config_Ini is used. we can see its _ construct.

First, determine whether a configuration file exists. The second is to manage the option. here, the option can be set with the allowModifications attribute (whether the attribute in the configuration file can be modified) and the nestSeparator attribute (the key separator in the configuration file, which defaults to the vertex ).

The following code calls $ iniArray = $ this-> _ loadIniFile ($ filename). this function is very important, that is, parsing the configuration file. When we followed in, we first called _ parseIniFile. in order not to make everyone messy, let's look at what the data returned by _ parseIniFile looks like:

The code is as follows:


Array
(
[General] => Array
(
[Url. base] =>/usvn
[Translation. locale] => zh_CN
[Timezone] => Asia/Shanghai
[System. locale] => aa_DJ.utf8
)

)


The last thing parsed is a two-dimensional array.

ParseIniFile actually calls the system function parse_ini_file for processing. Note that before and after calling parse_ini_file, it actually uses set_error_handler and restore_error_handler to expose the exception handling function. Because the configuration file parsing is very prone to errors, and the user prompt for this error should be very friendly, it is best to prompt the user to modify it there, therefore, Zend exposes error handling functions. If you want to design a friendly system, rewrite method _ loadFileErrorHandler in the inheritance class.

Continue from _ loadIniFile.

Because [] in our ini configuration file represents a setion, the key returned by the two-dimensional array returned by _ loadIniFile is general. However, if we use [general: 123] As the section in the configuration file, this function will return 123 as the val of [; extends. Actually, this is the case.

The code is as follows:


Array
(
[General] => Array
(
[; Extends] = & gt; 123
[Url. base] =>/usvn
[Translation. locale] => zh_CN
)

)


Now we return to _ construct. at this time, iniArray has obtained a two-dimensional array. if you set the section to be obtained, the iniArray will be processed _ arrayMergeRecursive, this mainly refers to the system in the key. locale => aa_DJ.utf8 to array (system => array (locale => aa_DJ.utf8 )). Here, the nestSeparator attribute in options is used. the default attribute is vertices, that is, translation. locale will be separated into arrays. for example, if the nestSeparator you passed in earlier is a colon, your configuration file should be set to translation: location = .. here we will not continue to catch up, which is nothing more than some string operations.

The last analyzed dataArray looks like this.

The code is as follows:


Array
(
[Url] => Array
(
[Base] =>/usvn
)

[Translation] => Array
(
[Locale] => zh_CN
)

[Timezone] => Asia/Shanghai
[System] => Array
(
[Locale] => aa_DJ.utf8
)
)


The following calls the constructor _ construct of the parent class. the parent class of Zend_Config_Ini is Zend_Config.


Class Zend_Config implements Countable, Iterator

Zend_Config implements the Countable interface (including the count () method) and Iterator interface (including current, key, next, rewind, valid, and other methods)

The constructor of Zend_Config puts the two-dimensional array analyzed above into _ data.


Here we focus on two functions.

_ Set and _ get

Magic method _ get ensures that the configuration value can be obtained using config-> field

The magic method _ set ensures whether the configuration file can be modified. _ allowModifications is used in the set. if this attribute is set, _ setter can be set, otherwise, the Zend_Config is read only exception will be thrown. allowModifications is also one of the attributes set in options.


Now, read the demo code at the beginning of the article.

Date_default_timezone_set ($ config-> timezone );

Here, we can use-> timezone to use the _ get instead of the attribute in config.
The Zend Config mechanism analysis is complete.

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.