Simple application of abstract class PHP abstract class

Source: Internet
Author: User
Tags mysql query
All right, the parent class postparent is defined as abstract, which specifies that the subclass must re-implement the Buildhtml () method, which does not have curly braces, and if there are any content will be an error.
Now the more you see the code is not necessary to use abstract class, with inheritance is also very chicken, well, there is nothing to say as if ...
In addition i separate MySQL outside, so calling method is very troublesome
1, first instantiate readarticle
2,mysql query, parameters from Readarticle::getsql ();
3, return the MySQL result resource to Readarticle::fetchresult ($result);
4,readarticle::buildhtml (); return HTML
If it's a list loop output, repeat the call 3 and 4.

Copy the Code code 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[' author '}At
{$this->timeago}


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


Eof
}
}

The above describes the abstract class of PHP abstract class simple application, including the content of the abstract class, I hope that the PHP tutorial interested in a friend to help.

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