Zend_Config_Xml usage analysis in ZendFramework tutorial

Source: Internet
Author: User
Tags xml attribute webhost zend framework
This article mainly introduces the Zend_Config_Xml usage in the ZendFramework tutorial, and analyzes the Zend_Config_Xml functions, usage methods, and related precautions in detail based on the instance form, for more information about Zend_Config_Xml in Zend Framework, see the following example. We will share this with you 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 the embedded object attribute syntax.

The root element of the XML file is irrelevant and can be named at will. The top-level XML element corresponds to the configuration data section.

The XML format supports hierarchical structure organization by embedding XML elements to section-level elements.

The XML element of leaf-level corresponds to the value of configuration data. The section inheritance is supported by a special XML attribute named extends. The value of this attribute corresponds to it through the extended section.

Return type

The configuration data read from Zend_Config_Xml always returns a string. Data Conversion from string to other types is left to developers to meet their specific needs.

Example: use Zend_Config_Xml

This example shows the basic usage of Zend_Config_Xml for loading configuration data from the INI file. In this example, there are production system and staging system configuration data. Because the development system configuration data is similar to the production system configuration data, the development system section inherits from the production system section. In this case, the result is arbitrary and can be reversed, that is, the production system section inherits from the development system section, although this cannot be used in more complex situations. Next, assume that the following configuration data is contained in/path/to/config. xml:

<?xml version="1.0"?>
   
      
   
    www.example.com
       
   
          pdo_mysql      
            
     
      db.example.com
             
     
      dbuser
             
     
      secret
             
     
      dbname
           
        
     
    
      
         
            
     
      dev.example.com
             
     
      devuser
             
     
      devsecret
           
        
     
  
 

Next, assume that the developer needs to retrieve the development configuration data from the XML file. This is very simple. you only need to 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"

For example, use the tag attribute in Zend_Config_Xml.

Zend_Config_Xml also supports two other methods to define nodes in the configuration file. They all use attributes. Because the extends and value attributes are reserved keywords (the latter is the second method for using attributes), they may not be used. The first method is to add the attribute to the parent node, which itself becomes a subnode:

<?xml version="1.0"?>
   
      
         
        
     
    
      
         
        
     
  
 

The other method does not make the configuration file smaller, but makes maintenance easy 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"?>
   
      
   
    www.example.com
       
               
            
             
             
             
           
        
     
    
      
         
            
             
             
           
        
     
  
 

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.