_php Example of zend_config_xml usage Analysis of Zend Framework Tutorial

Source: Internet
Author: User
Tags php programming xml attribute webhost zend zend framework

The examples in this article describe the Zend_config_xml usage in the Zend Framework. Share to everyone for your reference, specific as follows:

Zend_config_xml allows developers to store configuration data into a simple XML format and read it by embedding object attribute syntax.

The root element of the XML file is not relevant and can be named arbitrarily. The top-level XML element corresponds to the section of the configuration data.

The XML format supports hierarchical organization by embedding XML elements below the section-level element (Section-level elements).

The XML element of the leaf level (Leaf-level) corresponds to the value of the configuration data. Section inheritance is supported by a special XML attribute named extends, and the corresponding value of this property is inherited through an extension section (extending sections).

return type

The configuration data read into the Zend_config_xml always returns a string. Data from strings to other types of conversions is left to developers to adapt to their specific needs.

Example: Using Zend_config_xml

This example illustrates the basic use of Zend_config_xml to load configuration data from the INI file. In this example, there are configuration data for production systems (production system) and development systems (staging system). Because the development system configuration data is similar to the configuration data of the production system, the development system section inherits from the Production system section. In this case, the result (decision) is arbitrary and it can be reversed, that is, the production system section inherits from the development system section, although this is not possible for more complex scenarios. Next, assume that the following configuration data is included in the/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 
 

Next, assume that the developer needs to take development configuration data from the XML file. This is very simple, as long as you 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;  Output "dev.example.com"
echo $config->database->params->dbname;//Output "dbname"


Example: Using the Tag property in Zend_config_xml

Zend_config_xml also supports two other methods of defining nodes in the configuration file. They all take advantage of attributes. Because the extends and value properties are reserved keywords (the latter is the second method of using attributes), they may not be used. The first method uses a property to add a property 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 does not make the configuration file smaller, but it makes maintenance easier because you need to write a label name two times. You can create an empty tag that contains its values in the Value property:

<?xml version= "1.0"?>
<configdata>
  <production>
    <webhost>www.example.com </webhost>
    <database>
      <adapter value= "Pdo_mysql"/>
      <params>
         
 

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with your PHP programming based on the Zend Framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.