Zendframework Chapter 3 (Core Component-use configuration data to read data from the PHP configuration file)

Source: Internet
Author: User
Tags webhost

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 "<br> ";
Echo $ config-> webhost;
Echo "<br> ";
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)

<? 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.

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.