Application analysis of config mechanism based on Zend _php example

Source: Internet
Author: User
Tags error handling exception handling locale zend

Zend Config class in Zend_config_ini

Code
$config = new Zend_config_ini ("/var/www/html/usvn/config/config.ini", "General");

Date_default_timezone_set ($config->timezone);

Usvn_consoleutils::setlocale ($config->system->locale);

===

Config.ini File Contents

[General]

Url.base = "/USVN"

Translation.locale = "ZH_CN"

TimeZone = "Asia/shanghai"


Specific analysis
Only the Zend_config_ini constructor is used here, and we see it in the __construct.

The first is to determine if there is a configuration file. The second is to manage option, where option can be set with the Allowmodifications attribute (whether the properties in the configuration file can be modified), the Nestseparator property (the key separator in the configuration file, the default is the point).

The following is a call to $iniarray = $this->_loadinifile ($filename); This function is very important to parse the configuration file. Follow in, first call the _parseinifile, in order not to let everyone messy, we look at the return of _parseinifile data is what it looks like:

Copy Code code as follows:

Array
(
[General] => Array
(
[Url.base] =>/USVN
[Translation.locale] => ZH_CN
[TimeZone] => Asia/shanghai
[System.locale] => Aa_dj.utf8
)

)

The last thing to parse out is a two-dimensional array.

Parseinifile actually calls the system function parse_ini_file to handle it. In particular, it is noted that before and after calling Parse_ini_file it actually uses Set_error_handler and restore_error_handler to expose the function of the exception handling. Because when parsing the configuration file is very easy to error, and this error user hint should be very friendly, it is best to prompt the user to make changes there, so Zend deliberately expose the error handling function. If you want to design a very friendly system, override the method _loadfileerrorhandler in the inheriting class.

Keep watching from _loadinifile.

Because our INI configuration file uses [] to represent a setion, the key returned by the _loadinifile returned by the two-dimensional array is general. But in fact if we use [general:123] as a section in a configuration file, then this function returns 123 as a val of [; extends]. Actually, it is.

Copy Code code as follows:

Array
(
[General] => Array
(
[; extends] => 123
[Url.base] =>/USVN
[Translation.locale] => ZH_CN
)

)

Now back to the __construct, when Iniarray has acquired, is a two-dimensional array, the following if you set up to get section, you will iniarray processing _arraymergerecursive, The main thing is to change the System.locale => Aa_dj.utf8 in the key into array (system=> array (Locale=>aa_dj.utf8)). This is where the Nestseparator attribute in the options is used, and this property defaults to the point, Is that Translation.locale will be separated into groups, such as the nestseparator you passed in front of the colon, then your configuration file should be set to Translation:location =. It's not going to go down here, it's just some string manipulation.

The final analysis comes back to the DataArray is like this

Copy Code code as follows:

Array
(
[url] => Array
(
[Base] =>/USVN
)

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

[TimeZone] => Asia/shanghai
[System] => Array
(
[Locale] => Aa_dj.utf8
)
)

The constructor __construct of the parent class is called below, and Zend_config_ini's parent class is zend_config.


Class Zend_config implements countable, iterator

The zend_config implements the countable interface (including the Count () method), the iterator interface (including methods such as Current,key,next,rewind,valid)

The Zend_config constructor puts the two-dimensional array of the above analysis into the _data.


Here's a look at two functions

__set and __get

Magic Method __get guarantees that you can use Config->field to get the configuration value

Magic method __set to ensure that the configuration file can be modified, set in the use of _allowmodifications, if this property has a set, then __setter can be set, otherwise it will throw Zend_config is read only exception, Allowmodifications is also one of the properties set in options.


At this point, read the first demo code

Date_default_timezone_set ($config->timezone);

The reason for using->timezone here is to use __get instead of attributes in CONFIG.
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.