Simple Application of php abstract classes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.