php 抽象类的简单应用

yipeiwu_com5年前PHP代码库
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 class="post-title-div">
<h4>
<a href="http://foodstory.me/post.php?id={$this->fetchResult['id']}"
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写的获取各搜索蜘蛛爬行记录代码

那么下面分享一款用php写的获取各搜索蜘蛛爬行记录代码 支持搜索引擎如下 可以记录Baidu,Google,Bing,Yahoo,Soso,Sogou,Yodao爬行网站的记录! php...

php+xml实现在线英文词典查询的方法

本文实例讲述了php+xml实现在线英文词典查询的方法。分享给大家供大家参考。具体如下: 这里的xml相当于一个数据库。实现:查询某个英文单词,输出它的中文意思。 xml文件(数据库):...

解析PHP中intval()等int转换时的意外异常情况

先看看下面的网上的一个测试代码:复制代码 代码如下:<?php$a = 9.45*100;var_dump($a);var_dump(intval($a));$a = 945*1....

php数组添加与删除单元的常用函数实例分析

本文实例分析了php数组添加与删除单元的常用函数。分享给大家供大家参考。具体分析如下: <?php header("Content-type:text/html;cha...

PHP操作文件方法问答

PHP操作文件问答  前言:  PHP中对各类数据库的操作有着支持,对文件的操作也同样有着很丰富的操作方法,很多朋友现在的操作还是基于文件操作可是有的时候在操作文件的...