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;
}
}

相关文章

FCKeditor添加自定义按钮

在FCKeditor目录里的fckconfig.js打开,找到FCKConfig.ToolbarSets["Default"] 这里的设置是配置功能按钮的,你需要的留下,不需要的可以删掉...

PHP 防恶意刷新实现代码

复制代码 代码如下:<?php session_start(); $k=$_GET['k']; $t=$_GET['t']; $allowTime = 1800;//防刷新时间 $...

php魔法函数与魔法常量使用介绍

php 魔术函数 1。__construct() 实例化对象时被调用, 当__construct和以类名为函数名的函数同时存在时,__construct将被调用,另一个不被调用。 2。...

MongoDB在PHP中的常用操作小结

$mongodb = new Mongo(); //$connection = new Mongo( "$dburl:$port" ); // connect to a remote h...

微信公众平台之快递查询功能用法实例

本文实例讲述了微信公众平台之快递查询功能用法。分享给大家供大家参考。具体如下: 使用方法: #查快递(或三个首字母ckd)#快递编号#快递单号 如(查询EMS单号为10346164940...