All right: the postParent of the parent class is defined as abstract. It is required that the subclass must implement the buildHTML () method again. This method does not have curly brackets. If there is any content, an error will be reported.
The more I see it, the more I feel that this code does not need to use abstract classes. inheritance is also very rare. Well, there is nothing to say about it .....
In addition, I separated mysql from the outside, so it is very troublesome to call the method.
1. instantiate readArticle first
2. mysql query. The parameter is 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, you can call 3 and 4 again.
Copy codeThe 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 = <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>
<Time class = 'post-info-time' >{$ this-> timeAgo} </time>
</Div>
<Div class = "post-p-div">
{$ This-> fetchResult ['text']}
</Div>
</Div>
Eof;
}
}