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.