php 站點(diǎn)使用XML文件做配置類_PHP教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:php編寫的ACCESS處理類在做項(xiàng)目中要用到ACCESS數(shù)據(jù)庫,所以就寫了一個ACCESS處理類.函數(shù)名跟ADODB類一樣. ?php /* *ACCESS數(shù)據(jù)庫操作類 *2008-3-26 *LIQUAN *dsn = DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=.PATH_ROOT.databasedata.mdb; *conn=new Access(); *conn-Con
要用到,在網(wǎng)站上找了一個,然后自己整理成一個類..
<?php
/*
*PHP獲取和設(shè)置XML節(jié)點(diǎn),用于修改和讀取站點(diǎn)配置文件
*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();
//構(gòu)造函數(shù)
function __construct(configFile)
{
this->configFile=configFile;
this->parse();
}
//獲取屬性
function __get(id)
{
return this->items[id];
}
//設(shè)置屬性
function __set(key,value)
{
this->items[key]=value;
}
//解析XML文件保存到數(shù)組
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;
}
}
//保存XML文件
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);
}
}
?>
/*
*PHP獲取和設(shè)置XML節(jié)點(diǎn),用于修改和讀取站點(diǎn)配置文件
*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();
//構(gòu)造函數(shù)
function __construct(configFile)
{
this->configFile=configFile;
this->parse();
}
//獲取屬性
function __get(id)
{
return this->items[id];
}
//設(shè)置屬性
function __set(key,value)
{
this->items[key]=value;
}
//解析XML文件保存到數(shù)組
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;
}
}
//保存XML文件
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);
}
}
?>
分享:解析基于MVC的輕量級PHP框架做WEB開發(fā)已有一年,每次都寫重復(fù)的東西, 因此,想自己寫一下框架,以后開發(fā)方便. 本人之前學(xué)習(xí)asp.NET兩年,JSP半年,可是后來因?yàn)楣ぷ鞯脑蕮Q成PHP.其實(shí)很不喜歡PHP的語法.還有PHP的函數(shù)名,每回都忘記..還是喜歡C#和JAVA的語法,哈...不過PHP有PHP的優(yōu)點(diǎn),不像AS
相關(guān)PHP教程:
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時間
- PHP中獎概率的抽獎算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-php 站點(diǎn)使用XML文件做配置類
。