Home > Backend Development > PHP Tutorial > Zend Framework tutorial Zend_Config_Xml usage analysis, zendconfig.h_PHP tutorial

Zend Framework tutorial Zend_Config_Xml usage analysis, zendconfig.h_PHP tutorial

WBOY
Release: 2016-07-12 08:56:10
Original
805 people have browsed it

Zend Framework tutorial Zend_Config_Xml usage analysis, zendconfig.h

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:

<&#63;xml version="1.0"&#63;>
<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>

Copy after login

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"


Copy after login

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:

<&#63;xml version="1.0"&#63;>
<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>

Copy after login

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:

<&#63;xml version="1.0"&#63;>
<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>

Copy after login

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.

Articles you may be interested in:

  • Application analysis based on Zend’s Config mechanism
  • Zend Framework tutorial configuration file application.ini analysis
  • Zend Framework's method to implement multi-server sharing of SESSION data
  • Zend Framework Smarty extension implementation method
  • Zend Framework routing mechanism code analysis
  • Zend Framework implements session storage in memcache Method
  • Detailed explanation of usage of Zend Framework paging class
  • Zend Framework implementation of multi-file upload function example
  • Environment configuration for getting started with Zend Framework and the first Hello World example (with demo Source code download)
  • Zend Framework tutorial: How to connect to the database and perform add/delete queries (with demo source code download)
  • Zend Framework tutorial: Zend_Config_Ini usage analysis

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1113714.htmlTechArticleZend Framework tutorial Zend_Config_Xml usage analysis, zendconfig.h 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: Zen...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template