0

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?

David Ortega
  • 915
  • 9
  • 25
  • When you have a `static` method you can't use `this` -> `$this->formatDate` make `formatDate` static and use `self::formatDate` – ka_lin May 16 '16 at 16:17
  • You are calling the method statically `PostsMaster::listPost();`, this means the class is not instantiated which in turn means `__construct` isn't called and `$this` isn't available. Regarding your question you'd normally want to have the constructor public, and may have some private methods. But the reason to have properties and methods private is to make them unavailable outside the class. Meaning you would not be allowed to call a private method from your controller. – JimL May 16 '16 at 16:19
  • @JimL yes I don't want to allow $var = new PostsMaster(); as this opens an additional connection to an API and my calls get limited. (wasn't included on my example but is on the constructor function) so I cache return call and save it into the private variable. I don't know if there is another way of doing this – David Ortega May 16 '16 at 16:24

1 Answers1

0

The problem comes from the fact that you use "this" (an object qualifier) into a static method.

Instead you should use the static qualifier self.

public static function listPost(){
        $postList = [];
        //Query all posts
        $DBQuery = DB::getInstance(self::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;
    }
zoubida13
  • 1,738
  • 12
  • 20