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)用就可以了
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;
}
}
現(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;
}
}
您可能感興趣的文章:
- PHP 抽象方法與抽象類abstract關(guān)鍵字介紹及應(yīng)用
- PHP abstract與interface之間的區(qū)別
- 深入解讀php中關(guān)于抽象(abstract)類和抽象方法的問(wèn)題分析
- 詳談PHP中public,private,protected,abstract等關(guān)鍵字的用法
- php 接口類與抽象類的實(shí)際作用
- php接口和抽象類使用示例詳解
- PHP中抽象類、接口的區(qū)別與選擇分析
- PHP中抽象類和抽象方法概念與用法分析
- PHP入門(mén)教程之面向?qū)ο蟮奶匦苑治?繼承,多態(tài),接口,抽象類,抽象方法等)
- php抽象類使用要點(diǎn)與注意事項(xiàng)分析
- PHP abstract 抽象類定義與用法示例
相關(guān)文章
php中sprintf與printf函數(shù)用法區(qū)別解析
這篇文章主要介紹了php中sprintf與printf函數(shù)用法區(qū)別解析,需要的朋友可以參考下2014-02-02php 文件狀態(tài)緩存帶來(lái)的問(wèn)題
當(dāng)使用以下函數(shù)時(shí),會(huì)因?yàn)槲募顟B(tài)緩存,導(dǎo)致結(jié)果錯(cuò)誤2008-12-12屏蔽機(jī)器人從你的網(wǎng)站搜取email地址的php代碼
屏蔽機(jī)器人從你的網(wǎng)站搜取email地址然后發(fā)垃圾郵件的處理方法,需要的朋友可以參考下2012-11-11php通過(guò)前序遍歷樹(shù)實(shí)現(xiàn)無(wú)需遞歸的無(wú)限極分類
這篇文章主要介紹了php通過(guò)前序遍歷樹(shù)實(shí)現(xiàn)無(wú)需遞歸的無(wú)限極分類,涉及基于CI框架針對(duì)數(shù)據(jù)庫(kù)的查詢與遍歷操作,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07PHP+Ajax實(shí)現(xiàn)的檢測(cè)用戶名功能簡(jiǎn)單示例
這篇文章主要介紹了PHP+Ajax實(shí)現(xiàn)的檢測(cè)用戶名功能,結(jié)合簡(jiǎn)單實(shí)例形式分析了php結(jié)合ajax基于事件響應(yīng)動(dòng)態(tài)查詢數(shù)據(jù)庫(kù)及用戶名檢測(cè)相關(guān)操作技巧,需要的朋友可以參考下2019-02-02解讀PHP的Yii框架中請(qǐng)求與響應(yīng)的處理流程
這篇文章主要介紹了PHP的Yii框架中請(qǐng)求與響應(yīng)的處理流程,這也是Yii處理網(wǎng)站后臺(tái)的最基本功能,需要的朋友可以參考下2016-03-03PHP通過(guò)正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼
PHP通過(guò)正則表達(dá)式下載圖片到本地的實(shí)現(xiàn)代碼,主要是通過(guò)正則表達(dá)式匹配網(wǎng)頁(yè)內(nèi)容中的圖片地址并下載。2011-09-09