php 抽象类的简单应用

All right,父类postParent定义为抽象,规定子类必须重新实现 buildHTML()方法,这个方法并没有花括号,如果有不管有没有内容都会报错的。
现在越看越觉得这代码完全没必要用抽象类,用继承也都很鸡肋,好吧,也没啥好说的好像。。。。。
另外我把MysqL 分开在外面了,所以调用方法很麻烦
1,先实例化 readArticle
2,mySQL查询,参数来自 readArticle::getsql();
3,返回MysqL结果资源给 readArticle::fetchResult( $result );
4,readArticle::buildHTML(); 返回HTML
如果是列表循环输出的话,把 3 和 4 重复调用就可以了
<div class="codetitle"><a style="CURSOR: pointer" data="26203" class="copybut" id="copybut26203" onclick="doCopy('code26203')"> 代码如下:

<div class="codebody" id="code26203">
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">


<a href="http://foodstory.me/post.php?id={$this->fetchResult['id']}"
class="post-title-a" > {$this->fetchResult['title']}



<div class="post-info-div">
at


<div class="post-p-div">
{$this->fetchResult['text']}


eof;
}
}

抽象类

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...