Zendframework Learning Chapter III (Core components-application configuration data read from PHP config file)

Source: Internet
Author: User
Tags webhost
Zendframework Learning Chapter III (core components-reading data from PHP configuration files using configuration data)

Today and we learn with the use of configuration data, according to my understanding of this thing is to connect a database, save some configuration information, and then call to use. Of course, save configuration information is the configuration file has three kinds: 1, PHP proprietary configuration file 2, INI configuration file 3, XML configuration file The operation of these three files in the ZF is done by zend_config this component.

Reading data from a PHP configuration file

It is simple to use zend_config to read a PHP array or a configuration array in a normal PHP file.

let's start with the normal array , which is the name of the specified array variable as the Zend_config class instance object. Call the object property method to invoke the data in the array. As an example:

$array = Array (
' webhost ' = ' 127.0.0.1 ',
' Database ' =>array (
' db_host ' = ' localhost ',
' Db_user ' = ' root ',
' Db_pass ' = ' 123 ',
' db_name ' = ' test '
)
);
$config =new zend_config ($array); Use the specified array variable name as the parameter
echo "
";
echo $config->webhost;
echo "
";
echo $config->database->db_host;

Below is how to read the configuration information from the PHP file , the difference is that the return of the array in the configuration file line, the object is instantiated when the parameters directly use "require+php filename" on the line. Below is an example:

PHP File---(file name is test.php)

Return Array (
' webhost ' = ' 127.0.0.1 ',
' Database ' =>array (
' db_host ' = ' localhost ',
' Db_user ' = ' root ',
' Db_pass ' = ' 123 ',
' db_name ' = ' test '
)
);

?>

The file that reads the configuration information----

$filename = "test.php";

$config = new Zend_config (require $filename);

echo $config->webhost;

echo $config->database->db_user;

ok~! There's no problem. Read data from an array and read data from a php file parameter section Note that when reading from an array, the parameter is the name of the variable, and the data read from the PHP configuration file is its parameter 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.