您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

抽象类 php 抽象类的简单应用

2024/3/29 4:04:59发布8次查看
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 =select title, author, text, unixtime from post
where id = $id order by unixtime desc;
eof;
}
public function buildhtml()
{
return
fetchresult%5b'id'%5d%7d>class=post-title-a > {$this->fetchresult['title']}
{$this->fetchresult['author']} at
{$this->timeago}
{$this->fetchresult['text']}
eof;
}
}
以上就介绍了抽象类 php 抽象类的简单应用,包括了抽象类方面的内容,希望对php教程有兴趣的朋友有所帮助。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product