I'm currently building a kind of MVC PHP application to understand better MVC approach to development, but I'm presenting an issue.
My model class
<?php
//My super awesome model class for handling posts :D
class PostsMaster{
public $title;
public $content;
public $datePublished;
public $dateEdited;
private function __constructor($title, $content, $datePublished, $dateEdited){
$this->title = $title;
$this->content = $content;
$this->datePublished = $datePublished;
$this->dateEdited = $dateEdited;
}
private $something = 'eyey78425';
public static function listPost(){
$postList = [];
//Query all posts
$DBQuery = DB::getInstance($this->something);//Database PDO class :D
$DBQuery->query('SELECT * FROM Posts');
foreach($DBQuery as $post){
$postList[] = new PostsMaster($post['postTitle'], $post['postContent'], $this->formatDate($post['datePub']), $this->formatDate($post['dateEdit']));
}
return $postList;
}
private function formatDate($unformattedDate){
/* Formatting process */
return $formattedDate;
}
}
How I call it on the controller
<?php
require 'index.php';
function postList(){
require 'views/postList.php';
PostsMaster::listPost();
}
But when rendering I get this error:
fatal error using $this when not in object context...
I don't intend to make public formatDate function as I don't want it to be invoked outside, but how could I invoke it correctly in my code?