I would like to know the best way to setup my PHP Blog classes.

I have created a Post class which creates a blog post.

class Post

private $title;
private $body;
private $createdDate;
private $publishedDate = null;
private $published = false;

public function __construct(string $title, string $body)
    $this->createdDate = date('d/M/Y/');

public function publish()
    $this->published = true;
    $this->publishedDate = date('d/M/Y/');

public function unpublish()
    $this->published = false;
    $this->publishedDate = null;

public function setTitle(string $title)
    $this->title = $title;

public function getTitle(): string
    return $this->title;

public function setBody(string $body)
    $this->body = $body;

public function getBody(): string
    return $this->body;


I also have a simple Database class

class Database

private $db;

public function __construct($host, $name, $username, $password)
    $dsn = "mysql:host=$host;dbname=$name";
    try {
        $this->db = new PDO($dsn, $username, $password);
    } catch (PDOException $e) {
        die('Error: ' . $e->getMessage());

My question is where should the database class be extended from? I could use it in my Post class, but it wouldn't really make sense to make Post queries related to just this specific blog post. It also wouldn't make sense to perform a query to get all posts just from this object. The SOLID principle states a class should just have one single responsibility. Therefore I'm concerned that creating a post object and running multiple queries within the class is causing the class to have lots of responsibilities.

I have read about static methods where these could be of use, as I could run a query on a Post object, which wouldn't need to be instantiated. However, I have read that static methods should be avoided due to a variety of reasons, such as breaking encapsulation.

I have searched for examples online, but they don't seem to be implementing OOP completely. Any Pure OOP examples you know would be great to hear about.


closed as off-topic by 200_success, yuri, esote, Mast, Toby Speight Jun 3 at 10:25

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." – 200_success, yuri, esote, Mast, Toby Speight
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    \$\begingroup\$ It's a pity, but this question will be closed as off topic, because only a working code is accepted, and this code does literally nothing. To briefly answer your questions: get rid of the database class, it does nothing useful at the moments. Use vanilla PDO instead. Pass a PDO instance as a Post's constructor. parameter. Having an entity class is an accepted trade-off, called ActiveRecord. It is frowned upon nevertheless, and a better implementation would be of a Data Mapper. Shortly, a class that has methods to load from the database your Post objects and persist them back. \$\endgroup\$ – Your Common Sense Jun 2 at 15:39
  • \$\begingroup\$ Thanks for taking the time to help out here. I just want to make sure I am doing things the correct way. I will get some working code up so that I can get additional info. \$\endgroup\$ – Jason Donavon Jun 2 at 18:51
  • \$\begingroup\$ Yes, it would be the best. Just write any working prototype you can think of, and then we will gladly review it \$\endgroup\$ – Your Common Sense Jun 2 at 18:54

Browse other questions tagged or ask your own question.