Home  >  Article  >  Backend Development  >  Abstract class php simple application of abstract class

Abstract class php simple application of abstract class

WBOY
WBOYOriginal
2016-07-29 08:46:45950browse

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 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 the mysql result resource to readArticle::fetchResult( $result );
4, readArticle::buildHTML(); returns HTML
If it is a list loop output, just call 3 and 4 repeatedly

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




{$this->fetchResult['author']} at


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


eof;
}
}

The above introduces the simple application of abstract classes in PHP, including the content of abstract classes. I hope it will be helpful to friends who are interested in PHP tutorials.

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