The example in this article describes the usage of Zend_Config_Xml in Zend Framework. Share it with everyone for your reference, 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.
The leaf-level XML elements correspond to the values of the configuration data. Section inheritance is supported through a special XML attribute named extends, and the corresponding value of this attribute is inherited through the extending section.
Return type
Read into Zend_Config_Xml Configuration data in 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 that loads 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 the development configuration data from the 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_Xml
Zend_Config_Xml also supports two other methods in 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>
The other method also does not make the configuration file smaller, but makes maintenance easier, It's because 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>
I hope this article will be helpful to everyone in PHP programming based on the Zend Framework framework.
For more articles related to Zend Framework tutorial Zend_Config_Xml usage analysis, please pay attention to the PHP Chinese website!