Zend’s Config class is 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 content
[general]
url.base = "/usvn"
translation.locale = "zh_CN"
timezone = "Asia/Shanghai"
Detailed analysis
Only the constructor of Zend_Config_Ini is used here, and we see its __construct.
The first step is to determine whether there is a configuration file. The second step is to manage the options. The options here can be set with the allowModifications attribute (whether the attributes in the configuration file can be modified) and the nestSeparator attribute (the key separator in the configuration file, which defaults to dot).
The following is calling $iniArray = $this->_loadIniFile($filename); This function is very important because it parses the configuration file. Following up, we first call _parseIniFile. In order to avoid confusion, let’s take a look at what the data returned by _parseIniFile looks like:
Copy code Code As follows:
Array
(
[general] => Array
(
[url.base] => /usvn
[translation.locale ] => zh_CN
>The final thing parsed is a two-dimensional array.
parseIniFile actually calls the system function parse_ini_file for processing. Pay special attention here. Before and after calling parse_ini_file, it actually uses set_error_handler and restore_error_handler to expose the exception handling function. Because it is very easy to make errors when parsing configuration files, and the user prompt for this error should be very friendly, and it is best to prompt the user to make modifications there, so Zend deliberately exposes the error handling function. If you want to design a very friendly system, please override the method _loadFileErrorHandler in the inherited class.
Continue reading from _loadIniFile
Since [] is used in our ini configuration file to represent a set, the key returned by the two-dimensional array returned by _loadIniFile is general. But in fact, if we use [general:123] as section in the configuration file, then this function will return 123 as the val of [;extends]. It actually looks like this
Copy code
The code is as follows:
Array ( [general] => Array ( )
)
Now we are back to __construct. At this time, the iniArray has been obtained. It is a two-dimensional array. If you set the acquisition section below, the iniArray will be processed_arrayMergeRecursive, mainly Change system.locale => aa_DJ.utf8 in key to array(system=> array( locale=>aa_DJ.utf8)). The nestSeparator attribute in options is used here. This attribute defaults to dot, that is, translation.locale will be separated into an array. For example, the nestSeparator you passed in earlier is a colon, then your configuration file should be set to translation:location = .. I won’t continue to pursue it here, it is nothing more than some string operations.
The final analyzed dataArray looks like this
Copy the code
The code is as follows:
Array
(
[url] => > Array
(
[locale] => zh_CN
)
[timezone] => (
.
class Zend_Config implements Countable, Iterator
Zend_Config implements Countable interface (including count() method), 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
The 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 set. If this attribute is set, then __setter can be set. Otherwise, an exception of Zend_Config is read only will be thrown. allowModifications is also an option. One of the properties set in .
At this point, look at the demo code at the front of the article
date_default_timezone_set($config->timezone);
The reason why ->timezone can be used here is that __get is used instead of the attributes in config. The analysis of Zend’s Config mechanism is over.
http://www.bkjia.com/PHPjc/327070.html
www.bkjia.com
true
http: //www.bkjia.com/PHPjc/327070.html
TechArticle
Zend’s Config class is 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_Consol...