OOP in PHP. Exercising on creating a blog I am attempting to do object oriented programming in right way, and appreciate any review to see if my approach is correct.

class_blogpost.php as below:

&lt;?php
class BlogPosts {

function __construct() {
    # code...
}

public function ListAllBlogPosts() {
    $DB = DatabaseFactory::getFactory()-&gt;Connect();
    $query = $DB-&gt;prepare("SELECT * FROM posts ORDER BY date DESC");
    $query-&gt;execute();
    $list_all = $query-&gt;fetchAll(PDO::FETCH_ASSOC);
    return $list_all;
}

public function ShowABlogPost($post_id) {

    if (isset((int)$_GET["id"];)) {
        $post_id = (int)$_GET["id"];
        $DB = DatabaseFactory::getFactory()-&gt;Connect();
        $query = $DB-&gt;prepare("SELECT * FROM posts WHERE id = :post_id");
        $query-&gt;bindParam(':post_id', $post_id, PDO::PARAM_INT);
        $query-&gt;execute();
        $show_post = $query-&gt;fetchAll(PDO::FETCH_ASSOC);
        return $show_post[0];
    } else {
        echo "Post not found.";
    }

}

}

$blogposts = new BlogPosts;


Below is code for home.php which uses ListAllBlogPosts() function:

&lt;?php
include_once 'connect.php';
$title = "Home";
include_once 'header.php';
$list_all = $blogposts-&gt;ListAllBlogPosts();
?&gt;
      &lt;!-- col starts --&gt;
      &lt;div&gt;
&lt;?php
foreach ($list_all as $r) {
  $id = $r["id"];
  $title = $r["title"];
  $post = $r["post"];
  $date = $r["date"];
?&gt;

        &lt;h1&gt;&lt;?php echo $title; ?&gt;&lt;/h1&gt;
        &lt;p&gt;&lt;?php echo $post; ?&gt;&lt;/p&gt;
        &lt;a href="post.php?id=&lt;?php echo $id; ?&gt;"&gt;more&lt;/a&gt;

&lt;?php
} #ends foreach loop
?&gt;
      &lt;/div&gt;


And finally post.php below, user reaches as post.php?id=1 like that:

&lt;?php
include_once 'connect.php';
$id = $blogposts-&gt;ShowABlogPost($_GET["id"])["id"];
$title = $blogposts-&gt;ShowABlogPost($id)["title"];
$date = $blogposts-&gt;ShowABlogPost($id)["date"];
$post = $blogposts-&gt;ShowABlogPost($id)["post"];
include_once 'header.php';
?&gt;
      &lt;div&gt;
        &lt;h1&gt;&lt;?php echo $title; ?&gt;&lt;/h1&gt;
        &lt;p&gt;&lt;?php echo $post; ?&gt;&lt;/p&gt;
      &lt;/div&gt;
That's a good start but the structure is overall inefficient, that leads to code duplication.

the main problem is the violation of the Single responsibility principle. The ShowABlogPost does four things at a time:


connects to the database
checks the HTTP data
fetches a certain record from the database
displays some error


it should be doing only one thing, what it's intended for - fetching a record from a database. 

Another issue is multiple calls to ShowABlogPost(). It would be, like, if you have to pay a cab driver 44.50 and you got this exact amount in your pocket, but would fetch the notes and coins one by one instead of taking them all at once.

You already have an array that contains all the data from the first call. So just use this array.

Other issues are:


$DB = DatabaseFactory::getFactory()-&gt;Connect(); is duplicated in every method. Why not to make a database connection a class variable? So it will be available in all class methods by default.
PDO::FETCH_ASSOC could be made the default fetch option so you won't have to call it every time explicitly
when no variables are going to be used in the query, there is no use for the prepared statements. 
when you need to fetch only one row, then you have to use fetch() method, not fetchAll()


So let's rewrite your class

&lt;?php
class BlogPosts {

    protected $db;

    function __construct($db) {
        $this-&gt;db = $db;
    }

    public function ListAllBlogPosts() {
        $sql = "SELECT * FROM posts ORDER BY date DESC";
        $return $this-&gt;db-&gt;query($sql)-&gt;fetchAll();
    }

    public function ShowABlogPost($post_id) {
        $query = $this-&gt;db-&gt;prepare("SELECT * FROM posts WHERE id = :post_id");
        $query-&gt;bindParam(':post_id', $post_id, PDO::PARAM_INT);
        $query-&gt;execute();
        return $query-&gt;fetch();
    }
}


And now we can rewrite post.php

&lt;?php
include_once 'connect.php';
include_once 'class_blogpost.php';

$db = DatabaseFactory::getFactory()-&gt;Connect();
$blogposts = new BlogPosts($db);

if (!isset($_GET["id"]) {
    die( "Empty request");
} else {
    $post = $blogposts-&gt;ShowABlogPost($_GET["id"]);
}
?&gt;

&lt;?php if ($post) : ?&gt;
      &lt;div&gt;
        &lt;h1&gt;&lt;?= $post['title']; ?&gt;&lt;/h1&gt;
        &lt;p&gt;&lt;?= $post['post']; ?&gt;&lt;/p&gt;
      &lt;/div&gt;
&lt;?php else : ?&gt;
    Post not found.
&lt;?php endif ?&gt;