


Zend Framework introductory tutorial: Detailed explanation of the usage of Zend_Config component
Jan 05, 2017 am 10:33 AMThe example in this article describes the usage of Zend_Config component in Zend Framework. Share it with everyone for your reference, as follows:
1. Read data from PHP array
Use Zend_Config_Ini (read ini configuration file)
Zend_Config_Xml (read XML configuration file )
Case:
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Config'); $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); echo "Web服務(wù)器地址為:"; echo $config->webhost; echo "<p>"; echo "數(shù)據(jù)庫服務(wù)器名稱為:"; echo $config->database->db_host; echo "<p>"; echo "數(shù)據(jù)庫用戶名為:"; echo $config->database->db_user; echo "<p>"; echo "數(shù)據(jù)庫密碼為:"; echo $config->database->db_pass; echo "<p>"; echo "數(shù)據(jù)庫名稱為:"; echo $config->database->db_name; echo "<p>";
Result:
Web server address is: 127.0.0.1
Database server name For: localhost
The database user name is: root
The database password is: 123
The database name is: test
Description:
When initializing the Zend_Config class, add it to the array The data directly becomes the attribute value of the $config object.
2. Read data from the PHP configuration file
Case:
(1) First is the configuration file, the code is as follows.
<?php return array( 'webhost' => '127.0.0.1', 'database' => array( 'db_host' => 'localhost', 'db_user' => 'root', 'db_pass' => '123', 'db_name' => 'test' ) );
(2) Create a PHP file that reads the configuration, the code is as follows.
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Config'); $filename = 'config.php'; //定義配置文件名 $config = new Zend_Config(require $filename); //為類實(shí)例化對(duì)象 echo "Web服務(wù)器地址為:"; echo $config->webhost; echo "<p>"; echo "數(shù)據(jù)庫服務(wù)器名稱為:"; echo $config->database->db_host; echo "<p>"; echo "數(shù)據(jù)庫用戶名為:"; echo $config->database->db_user; echo "<p>"; echo "數(shù)據(jù)庫密碼為:"; echo $config->database->db_pass; echo "<p>"; echo "數(shù)據(jù)庫名稱為:"; echo $config->database->db_name; echo "<p>";
Explanation: The result is the same as before, except that this time the read data is obtained from the php file. The generally required configuration information is nothing more than database name, user name, password, etc.
3. Read data from the ini file
The subclass Zend_Config_Ini of the Zend_Config component allows reading data from the ini configuration file.
Case:
(1) Create an ini configuration file for the content to be read, the code is as follows.
[database] hostname = localhost database.type = mysql database.host = localhost database.user = root database.pass = 123 database.name = test_ini
Save it as config.ini
(2) Create a PHP file that reads the contents of the configuration file. The code is as follows.
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Config_Ini'); $filename = 'config.ini'; //定義配置文件名 $config = new Zend_Config_Ini($filename,'database'); //為類實(shí)例化對(duì)象 echo "數(shù)據(jù)庫服務(wù)器名稱為:"; echo $config->hostname; echo "<p>"; echo "數(shù)據(jù)庫類型為:"; echo $config->database->type; echo "<p>"; echo "數(shù)據(jù)庫用戶名為:"; echo $config->database->user; echo "<p>"; echo "數(shù)據(jù)庫密碼為:"; echo $config->database->pass; echo "<p>"; echo "數(shù)據(jù)庫名稱為:"; echo $config->database->name; echo "<p>";
Note: After instantiating the object, specify the corresponding parameters, and then you can use the information in the configuration file.
The result is:
The database server name is: localhost
The database type is: mysql
The database user name is: root
The database password is: 123
Database The name is: test_ini
4. Call the data read in ini in the form of an array
Use toArray() method to achieve conversion
Case:
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Config_Ini'); $filename = 'config.ini'; //定義配置文件名 $config = new Zend_Config_Ini($filename,'database'); //為類實(shí)例化對(duì)象 $temp = $config->database->toArray(); //使得其中一個(gè)屬性的數(shù)據(jù)轉(zhuǎn)為數(shù)組,并將數(shù)據(jù)賦給變量$temp echo "數(shù)據(jù)庫類型為:"; echo $temp[type]; echo "<p>"; echo "數(shù)據(jù)庫用戶名為:"; echo $temp[user]; echo "<p>"; echo "數(shù)據(jù)庫密碼為:"; echo $temp[pass]; echo "<p>"; echo "數(shù)據(jù)庫名稱為:"; echo $temp[name]; echo "<p>";
Result:
The database type is: mysql
The database user name is: root
The database password is: 123
The database name is :test_ini
5. Read data from XML configuration file
Achieved through subclass Zend_Config_Xml
root element top element
section-level elements section element
leaf elements leaf element
Case:
(1) Create an XML file
<?xml version = '1.0'?> <config> <production> <webhost>127.0.0.1</webhost> <database> <type>pdo_mysql</type> <host>localhost</host> <username>db_user</username> <password>db_pass</password> <name>test</name> </database> </production> <stag extends="production"> <database> <host>localhost</host> <username>db_user</username> <password>db_pass</password> </database> </stag> </config>
Name it config.xml
(2) Create a PHP file that reads XML file data. The code is as follows.
<?php require_once("Zend/Loader.php"); Zend_Loader::loadClass('Zend_Config_Xml'); $filename = 'config.xml'; //定義配置文件名 $section = "stag"; //定義需要加載的節(jié)名 $config = new Zend_Config_Xml($filename,$section); //為類實(shí)例化對(duì)象 echo "服務(wù)器地址為:"; echo $config->webhost; echo "<p>"; echo "數(shù)據(jù)庫類型為:"; echo $config->database->type; echo "<p>"; echo "數(shù)據(jù)庫用戶名為:"; echo $config->database->username; echo "<p>"; echo "數(shù)據(jù)庫密碼為:"; echo $config->database->password; echo "<p>";
Result:
The server address is: 127.0.0.1
The database type is: pdo_mysql
The database user name is: db_user
The database password is: db_pass
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.
For more detailed explanations on the usage of the Zend_Config component in the Zend Framework introductory tutorial, please pay attention to the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)