不同文件構(gòu)建PHP程序的方式(2)_PHP教程
推薦:PHP集成百度Ueditor 1.4.3最近很多群友都來問我怎么集成百度UE(ueditor 1.4.3),實在回答不過來,所以在這寫一下集成百度UE的思路,本文內(nèi)使用的最新版的UE1.4.3。 下載安裝 1.首先到官網(wǎng)下載最新版的UE1.4.3 UE官方下載地址:http://ueditor.baidu.com/website/download.html#ueditor 這里我下
盡管文本文件易于閱讀及編輯,但卻不如 XML 文件流行。另外,XML 有眾多適用的編輯器,這些編輯器能夠理解標(biāo)記、特殊符號轉(zhuǎn)義等等。所以配置文件的 XML 版本會是什么樣的呢?清單 11 顯示了 XML 格式的配置文件。
清單 11. config.xml
清單 12 顯示了使用 XML 來裝載配置設(shè)置的 Configuration 類的更新版。
清單 12. xml1.php
class Configuration
{
private $configFile = ‘config.xml’;
private $items = array();
function __construct() { $this->parse(); }
function __get($id) { return $this->items[ $id ]; }
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;
}
}
$c = new Configuration();
echo( $c->TemplateDirectory.” ” );
?>
看起來 XML 還有另一個好處:代碼比文本版的代碼更為簡潔、容易。為保存這個 XML,需要另一個版本的 save 函數(shù),將結(jié)果保存為 XML 格式,而不是文本格式。
清單 13. xml2.php
…
function save()
{
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( “config” );
$doc->appendChild( $r );
foreach( $this->items as $k => $v )
{
$kn = $doc->createElement( $k );
$kn->appendChild( $doc->createTextNode( $v ) );
$r->appendChild( $kn );
}
copy( $this->configFile, $this->configFile.’.bak’ );
$doc->save( $this->configFile );
}
…
這段代碼創(chuàng)建了一個新的 XML 文檔對象模型(Document Object Model ,DOM),然后將 $items 數(shù)組中的所有數(shù)據(jù)都保存到這個模型中。完成這些以后,使用 save 方法將 XML 保存為一個文件。
使用數(shù)據(jù)庫
最后的替代方式是使用一個數(shù)據(jù)庫保存配置元素的值。那首先要用一個簡單的模式來存儲配置數(shù)據(jù)。下面是一個簡單的模式。
清單 14. schema.sql
DROP TABLE IF EXISTS settings;
CREATE TABLE settings (
id MEDIUMINT NOT NULL AUTO_INCREMENT,
name TEXT,
value TEXT,
PRIMARY KEY ( id )
);
這要求進(jìn)行一些基于應(yīng)用程序需求的調(diào)整。例如,如果想讓配置元素按照每個用戶進(jìn)行存儲,就需要添加用戶 ID 作為額外的一列。
為了讀取及寫入數(shù)據(jù),我編寫了如圖 15 所示的更新過的 Configuration 類。
清單 15. db1.php
require_once( ‘DB.php’ );
$dsn = ‘MySQL://root:passWord@localhost/config’;
$db =& DB::Connect( $dsn, array() );
if (PEAR::isError($db)) { die($db->getMessage()); }
class Configuration
{
private $configFile = ‘config.xml’;
private $items = array();
function __construct() { $this->parse(); }
function __get($id) { return $this->items[ $id ]; }
function __set($id,$v)
{
global $db;
$this->items[ $id ] = $v;
$sth1 = $db->prepare( ‘DELETE FROM settings WHERE name=?’ );
$db->execute( $sth1, $id );
if (PEAR::isError($db)) { die($db->getMessage()); }
$sth2 = $db->prepare(
‘INSERT INTO settings ( id, name, value ) VALUES ( 0, ?, ? )’ );
$db->execute( $sth2, array( $id, $v ) );
if (PEAR::isError($db)) { die($db->getMessage()); }
}
function parse()
{
global $db;
$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;
$res = $db->query( ‘SELECT name,value FROM settings’ );
if (PEAR::isError($db)) { die($db->getMessage()); }
while( $res->fetchInto( $row ) ) {
$this->items[ $row[0] ] = $row[1];
}
}
}
$c = new Configuration();
echo( $c->TemplateDirectory.” ” );
$c->TemplateDirectory = ‘new foo’;
echo( $c->TemplateDirectory.” ” );
?>
分享:PHP間隔一段時間執(zhí)行代碼的方法本文實例講述了PHP間隔一段時間執(zhí)行代碼的方法。分享給大家供大家參考。具體分析如下: PHP如何設(shè)置每隔一段時間自動執(zhí)行某段代碼?例如定時生成靜態(tài)文件之類的,這就需要設(shè)置休眠時間,即每隔一段時間程序就會調(diào)用某段代碼. 代碼如下: 復(fù)制代碼代碼如下:ignore_user_abor
- 相關(guān)鏈接:
- 教程說明:
PHP教程-不同文件構(gòu)建PHP程序的方式(2)
。