Home >Backend Development >PHP Tutorial >Zend Framework tutorial Zend_Config_Xml usage analysis, zendconfig.h_PHP tutorial
This article describes the usage of Zend_Config_Xml in Zend Framework with examples. Share it with everyone for your reference, the details are as follows:
Zend_Config_Xml allows developers to store configuration data in a simple XML format and read it through embedded object attribute syntax.
The root element of the XML file is irrelevant and can be named arbitrarily. Top-level XML elements correspond to sections of configuration data.
The XML format supports hierarchical organization by embedding XML elements below section-level elements.
Leaf-level XML elements correspond to the values of configuration data. Section inheritance is supported through a special XML attribute called extends, and the corresponding value of this attribute is inherited through the extending section.
Return Type
Reading configuration data in Zend_Config_Xml always returns a string. Conversion of data from strings to other types is left to developers to adapt to their specific needs.
Example: Using Zend_Config_Xml
This example illustrates the basic usage of Zend_Config_Xml to load configuration data from an INI file. In this example there is configuration data for the production system and the staging system. Because the development system configuration data is similar to the production system configuration data, the development system's sections inherit from the production system's sections. In this case, the decision is arbitrary and it can be done the other way around, with the production system section inheriting from the development system section, although this is not possible for more complex cases. Next, assume that the following configuration data is contained in /path/to/config.xml:
<?xml version="1.0"?> <configdata> <production> <webhost>www.example.com</webhost> <database> <adapter>pdo_mysql</adapter> <params> <host>db.example.com</host> <username>dbuser</username> <password>secret</password> <dbname>dbname</dbname> </params> </database> </production> <staging extends="production"> <database> <params> <host>dev.example.com</host> <username>devuser</username> <password>devsecret</password> </params> </database> </staging> </configdata>
Next, assume that the developer needs to obtain development configuration data from an XML file. This is very simple, just specify the XML file and the development system section to load the data:
$config = new Zend_Config_Xml('/path/to/config.xml', 'staging'); echo $config->database->params->host; // 输出 "dev.example.com" echo $config->database->params->dbname; // 输出 "dbname"
Example: Using the tag attribute
in Zend_Config_XmlZend_Config_Xml also supports two other methods to define nodes in the configuration file. They all make use of properties. Because the extends and value attributes are reserved keywords (the latter is the second way to use attributes), they may not be used. The first way to use attributes is to add the attribute to the parent node, which itself becomes a child node:
<?xml version="1.0"?> <configdata> <production webhost="www.example.com"> <database adapter="pdo_mysql"> <params host="db.example.com" username="dbuser" password="secret" dbname="dbname"/> </database> </production> <staging extends="production"> <database> <params host="dev.example.com" username="devuser" password="devsecret"/> </database> </staging> </configdata>
Another method that also makes the configuration file smaller, but makes maintenance easier, is that you need to write the tag name twice. You can create an empty tag that contains its value in the value attribute:
<?xml version="1.0"?> <configdata> <production> <webhost>www.example.com</webhost> <database> <adapter value="pdo_mysql"/> <params> <host value="db.example.com"/> <username value="dbuser"/> <password value="secret"/> <dbname value="dbname"/> </params> </database> </production> <staging extends="production"> <database> <params> <host value="dev.example.com"/> <username value="devuser"/> <password value="devsecret"/> </params> </database> </staging> </configdata>
Readers who are interested in more zend-related content can check out the special topics of this site: "Zend FrameWork Framework Introductory Tutorial", "php Excellent Development Framework Summary", "Yii Framework Introduction and Summary of Common Techniques", "ThinkPHP Introductory Tutorial" , "php object-oriented programming introductory tutorial", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP programming based on the Zend Framework framework.