Home >Backend Development >PHP Tutorial >Simple application of php abstract class_PHP tutorial

Simple application of php abstract class_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:24:36873browse

All right, the parent class postParent is defined as abstract, which stipulates that subclasses must reimplement the buildHTML() method. This method does not have curly braces. If there are curly braces, an error will be reported regardless of whether there is content or not.
The more I look at it now, the more I feel that there is no need to use abstract classes in this code, and inheritance is useless. Well, there is nothing good to say. . . . .
In addition, I separated mysql outside, so calling the method is very troublesome
1, instantiate readArticle first
2, mysql query, the parameters come from readArticle::getSQL();
3, return mysql The result resource is given to readArticle::fetchResult( $result );
4, readArticle::buildHTML(); returns HTML
If it is a list loop output, just call 3 and 4 repeatedly

Copy code The code is as follows:

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



class="post-title-a" > {$this->fetchResult['title ']}





{$this->fetchResult['text'] }


eof;
}
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324297.htmlTechArticleAll right, the parent class postParent is defined as abstract, which stipulates that the subclass must reimplement the buildHTML() method. This method does not There are no curly braces. If there are any, an error will be reported regardless of whether there is any content. Now...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn