Simple application _php Techniques for PHP abstract classes

Source: Internet
Author: User
All right, the parent class postparent is defined as an abstraction, and the subclass must implement the Buildhtml () method, which does not have curly braces, and if there is no content, it will be an error.
Now the more I think this code is completely unnecessary to use abstract classes, with inheritance are also very chicken, well, there is nothing to say as if ...
In addition, I separate MySQL from the outside, so calling the 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, call 3 and 4 again.
Copy 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 =<<<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" >

<a href= "http://foodstory.me/post.php?id={$this->fetchresult[' id '}"
class= "Post-title-a" > {$this->fetchresult[' title '}
</a>

</div>
<div class= "Post-info-div" >
<span class= ' post-info-author ' >{$this->fetchresult[' author ']}</span> at
<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.