search
Homephp教程PHP开发Zend Framework tutorial Zend_Config_Xml usage analysis

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.

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

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 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 get the development configuration from the XML file data. This is very simple, just specify the XML file and the development system section to load the data:

$config = new Zend_Config_Xml(&#39;/path/to/config.xml&#39;, &#39;staging&#39;);
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 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>

The other method will not Configuration files become smaller, but make maintenance easier 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 help you design PHP programs based on the Zend Framework framework. Helps.

For more articles related to Zend Framework tutorial Zend_Config_Xml usage analysis, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.