Simple application of php abstract classes

Source: Internet
Author: User
I think the list of articles in the blog is consistent with that of a single article. I think the HTML code is different except for the SQL query statement structure. However, the two are the main functions, so this is indeed a bit unsuitable, but last night I read a little design pattern, All right, the parent class postParent 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 = < SELECT title, author, text, unixtime FROM post
WHERE id = $ id order by unixtime DESC;
Eof;
}
Public function buildHTML ()
{
Return <


FetchResult ['id']}"
Class = "post-title-a" >{$ this-> fetchResult ['title']}




{$ This-> fetchResult ['autor']}
{$ This-> timeAgo}


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


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.