国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發(fā) > PHP > 正文

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

2024-05-04 23:16:58
字體:
供稿:網(wǎng)友
All right, 父類postParent定義為抽象,規(guī)定子類必須重新實(shí)現(xiàn) buildHTML()方法,這個(gè)方法并沒有花括號,如果有不管有沒有內(nèi)容都會報(bào)錯(cuò)的。
現(xiàn)在越看越覺得這代碼完全沒必要用抽象類,用繼承也都很雞肋,好吧,也沒啥好說的好像。。。。。
另外我把mysql 分開在外面了,所以調(diào)用方法很麻煩
1,先實(shí)例化 readArticle
2,mysql查詢,參數(shù)來自 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>
<div>
<h4>
<a
class="post-title-a" > {$this->fetchResult['title']}
</a>
</h4>
</div>
<div>
<span>{$this->fetchResult['author']}</span> at
<time>{$this->timeAgo}</time>
</div>
<div>
{$this->fetchResult['text']}
</div>
</div>
eof;
}
}

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 明光市| 横峰县| 日土县| 当涂县| 辽阳县| 泌阳县| 土默特左旗| 桂林市| 英超| 衢州市| 区。| 伊春市| 德格县| 中阳县| 河间市| 兰坪| 武功县| 江油市| 永安市| 蒙山县| 麦盖提县| 湘潭市| 屏东市| 揭阳市| 东光县| 鸡西市| 泰和县| 宁夏| 温州市| 五华县| 沈丘县| 中江县| 长泰县| 翁牛特旗| 永登县| 张家川| 岱山县| 嫩江县| 盐城市| 龙川县| 五寨县|