php 抽象类的简单应用

yipeiwu_com6年前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数组内存耗用太多问题的解决方法

因为这个是语言的问题,常规方案比较难以解决。下面是一种通过string 解决的方案。 复制代码 代码如下: $total = 100000; $double = ""; for ($i...

一段实用的php验证码函数

本文实例为大家分享了几段php验证码函数,都很实用,具体内容如下 代码段一:简单php验证码函数 <?php function code(){ $im =...

如何对PHP程序中的常见漏洞进行攻击(上)第1/2页

之所以翻译这篇文章,是因为目前关于CGI安全性的文章都是拿Perl作为例子,而专门介绍ASP,PHP或者JSP安全性的文章则很少。Shaun Clowes的这篇文章比较全面地介...

phpmyadmin安装时提示:Warning: require_once(./libraries/common.inc.php)错误解决办法

比较简洁的方法:新建一个目录将phpmyadmin中的文件复制到这个目录中,iis中设置一下路径。 安装好PHPmyAdmin后,在IE里访问时会出现下面的这种错误: Warning:...

相对路径转化成绝对路径

提取 Gregarius中的一个函数。可以把网页中的相对路径自动转化成绝对路径。 <?  function relative_to_absolute(...