Chapter 3 of ZendFramework learning (core component-use configuration data to read data from the php configuration file) today I will learn how to use configuration data with you, according to my understanding of this thing, that is, connecting to a database, saving some configuration information, and then calling it for use. Of course, there are three configuration files to save the configuration information: 1. php proprietary configuration file 2. ini ZendFramework learning Chapter 3 (core component-use configuration data to read data from the php configuration file)
Today, I am going to work with you to learn how to use configuration data. my understanding of this is where to connect to a database, save some configuration information, and then call it for use. Of course, there are three configuration files to save the configuration information: 1. php proprietary configuration file 2. ini configuration file 3. operations on these three files in the XML configuration file ZF are completed by the Zend_Config component.
Read data from the php configuration file
It is easy, that is, to use Zend_Config to read the php array or the configuration array in the common php file.
Let's talk about a common array, that is, when it is a Zend_Config class instance object, use the specified array variable name as this parameter. The object attribute method is used to call data in the array. For example:
$ Array = array (
'Webhost' => '2017. 0.0.1 ',
'Database' => array (
'Db _ host' => 'localhost ',
'Db _ user' => 'root ',
'Db _ pass' => '123 ',
'Db _ name' => 'test'
)
);
$ Config = new Zend_Config ($ array); use the name of the specified array variable as this parameter.
Echo"
";
Echo $ config-> webhost;
Echo"
";
Echo $ config-> database-> db_host;
The following describes how to read configuration information from the PHP file. The difference is that the array is returned in the configuration file. When instantiating an object, the parameter directly uses"Require + php file name"That's all. Below is an example:
Php file --- (File name: test. php)
Return array (
'Webhost' => '2017. 0.0.1 ',
'Database' => array (
'Db _ host' => 'localhost ',
'Db _ user' => 'root ',
'Db _ pass' => '123 ',
'Db _ name' => 'test'
)
);
?>
File for reading configuration information ----
$ Filename= "Test. php ";
$ Config = new Zend_Config (require $ filename );
Echo $ config-> webhost;
Echo $ config-> database-> db_user;
OK ~! No problem. read data from the array and read data parameters from the PHP file. Note that when reading data from the array, the parameter is the name of the array variable, the parameter for reading data from the php configuration file is require + file name.