Php site uses XML files for configuration class-PHP source code

Source: Internet
Author: User
Ec (2); To be used, find one on the website and organize it into a class... & nbsp; & lt ;? Php ** PHP obtains and sets XML nodes for modifying and reading site configuration files * & nbsp; * LIQUAN * eg. get & nbsp; config & nbsp; * $ c & nbsp; new & nbsp; Configuration (config. xml); * echo (& nb script ec (2); script

To use it, find one on the website and organize it into a class ..



/*
* PHP obtains and sets XML nodes for modifying and reading site configuration files
* 2008-4-3
* LIQUAN
* Eg. get config
* $ C = new Configuration (''config. xml '');
* Echo ($ c-> TemplateDirectory ."");
*
* Set config
* $ C = new Configuration (''config. xml '');
* $ C-> TemplateDirectory = ''test '';
* $ C-> save ();
*/

Class Configuration
{
Private $ configFile;
Private $ items = array ();

// Constructor
Function _ construct ($ configFile)
{
$ This-> configFile = $ configFile;
$ This-> parse ();
}

// Get attributes
Function _ get ($ id)
{
Return $ this-> items [$ id];
}

// Set attributes
Function _ set ($ key, $ value)
{
$ This-> $ items [$ key] = $ value;
}

// Parse the XML file and save it to an array
Function parse ()
{
$ Doc = new DOMDocument ();
$ Doc-> load ($ this-> configFile );
$ Cn = $ doc-> getElementsByTagName ('config '');
$ Nodes = $ cn-> item (0)-> getElementsByTagName (''*'');
Foreach ($ nodes as $ node)
{
$ This-> items [$ node-> nodeName] = $ node-> nodeValue;
}
}

// Save the XML file
Function save ()
{
$ Doc = new DOMDocument ();
$ Doc-> formatOutput = true;

$ R = $ doc-> createElement (''config '');
$ Doc-> appendChild ($ r );

Foreach ($ this-> items as $ k => $ v)
{
$ KeyName = $ doc-> createElement ($ k );
$ KeyName-> appendChild ($ doc-> createTextNode ($ v ));
$ R-> appendChild ($ keyName );
}
Copy ($ this-> configFile, $ this-> configFile. ". bak ");

$ Doc-> save ($ this-> configFile );
}

}


?>



</

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.