欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php 抽象類的簡(jiǎn)單應(yīng)用

 更新時(shí)間:2011年09月06日 23:22:07   作者:  
我想博客中的 文章列表和單個(gè)的文章閱讀 統(tǒng)一起來(lái),我覺(jué)得除了sql查詢語(yǔ)句結(jié)構(gòu)不同,HTML代碼不同,其它也就一樣。不過(guò)話說(shuō)回來(lái),這兩個(gè)都是主要功能,所以這的確有點(diǎn)不適合,不過(guò)昨晚讀了一點(diǎn)設(shè)計(jì)模式,好歹得寫(xiě)點(diǎn)啥好。
All right, 父類postParent定義為抽象,規(guī)定子類必須重新實(shí)現(xiàn) buildHTML()方法,這個(gè)方法并沒(méi)有花括號(hào),如果有不管有沒(méi)有內(nèi)容都會(huì)報(bào)錯(cuò)的。
現(xiàn)在越看越覺(jué)得這代碼完全沒(méi)必要用抽象類,用繼承也都很雞肋,好吧,也沒(méi)啥好說(shuō)的好像。。。。。
另外我把mysql 分開(kāi)在外面了,所以調(diào)用方法很麻煩
1,先實(shí)例化 readArticle
2,mysql查詢,參數(shù)來(lái)自 readArticle::getSQL();
3,返回mysql結(jié)果資源給 readArticle::fetchResult( $result );
4,readArticle::buildHTML(); 返回HTML
如果是列表循環(huán)輸出的話,把 3 和 4 重復(fù)調(diào)用就可以了
復(fù)制代碼 代碼如下:

abstract class postParent
{
protected $querySQL;
public $fetchResult;
public $timeAgo; // eg : 2 days ago
abstract protected function buildHTML();
public function getSQL()
{
return $this->querySQL;
}
public function fetchResult( $result )
{
$this->fetchResult = mysql_fetch_assoc( $result );
}
public function error()
{}
}
class readArticle extends postParent
{
public function __construct( $id )
{
$this->querySQL =<<<eof
SELECT title, author, text, unixtime FROM post
WHERE id = $id ORDER BY unixtime DESC;
eof;
}
public function buildHTML()
{
return <<<eof
<div id="post-text">
<div class="post-title-div">
<h4>
<a
class="post-title-a" > {$this->fetchResult['title']}
</a>
</h4>
</div>
<div class="post-info-div">
<span class='post-info-author'>{$this->fetchResult['author']}</span> at
<time class='post-info-time'>{$this->timeAgo}</time>
</div>
<div class="post-p-div">
{$this->fetchResult['text']}
</div>
</div>
eof;
}
}

相關(guān)文章

最新評(píng)論