手机
当前位置:查字典教程网 >编程开发 >php教程 >php 抽象类的简单应用
php 抽象类的简单应用
摘要:Allright,父类postParent定义为抽象,规定子类必须重新实现buildHTML()方法,这个方法并没有花括号,如果有不管有没有...

All right, 父类postParent定义为抽象,规定子类必须重新实现 buildHTML()方法,这个方法并没有花括号,如果有不管有没有内容都会报错的。

现在越看越觉得这代码完全没必要用抽象类,用继承也都很鸡肋,好吧,也没啥好说的好像。。。。。

另外我把mysql 分开在外面了,所以调用方法很麻烦

1,先实例化 readArticle

2,mysql查询,参数来自 readArticle::getSQL();

3,返回mysql结果资源给 readArticle::fetchResult( $result );

4,readArticle::buildHTML(); 返回HTML

如果是列表循环输出的话,把 3 和 4 重复调用就可以了

复制代码 代码如下:

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>

<h4>

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

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;

}

}

【php 抽象类的简单应用】相关文章:

php函数重载的替代方法

PHP生成器简单实例

php curl 获取https请求的2种方法

php简单smarty入门程序实例

php 批量查询搜狗sogou代码分享

php比较相似字符串的方法

PHP获取数组的键与值方法小结

PHP使用者状态管理功能的应用

php实现递归抓取网页类实例

php中PDO方式实现数据库的增删改查

精品推荐
分类导航