Call to a member function on null?

I have done all sorts of things to understand what is actually wrong with the code, but I just couldn’t figure out what was causing this error.

Below is class called Topic :

class Topic {

        // Init DB variables
        private $db;

        *   Constructor
        public function __contruct() {
            $this->db = new Database();


        *   Get All Topics
        public function getAllTopics() {

            $this->db->query("SELECT topics.*, users.username, 
                              FROM topics 
                              INNER JOIN users 
                              ON topics.user_id = 
                              INNER JOIN categories 
                              ON topics.category_id = 
                              ORDER BY create_date DESC");

            // Assign the results
            $results = $this->db->resultset();

            return $results;

When I create an object of class Topic,

$topic = new Topic();
$results = $topic->getAllTopics();

there is always an error like this :

Fatal error: Call to a member function query() on null in
on line 25 i.e. $this->db->query(…)

The Database class is working perfectly as I have run tests independently for that class, and there is no problem with it.

Here is the Database class :


    * Using PDO for the first time in my life.

    class Database  {

        private $host = DB_HOST;
        private $user = DB_USER;
        private $pass = DB_PASS;
        private $dbname = DB_NAME;

        private $dbh;
        private $error;
        private $stmt;

        public function __construct()   {
            // Set DSN
            $dsn = 'mysql:host=' . $this->host . ';dbname=' . $this->dbname;

            // Set options
            $options = array(
                PDO::ATTR_PERSISTENT => true, 

            // Create a new PDO instance
            try { 
                $this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
            // Catch the errors using exceptions handler
            catch(PDOException $e){
                $this->error = $e->getMessage();

        public function query($query){
            $this->stmt = $this->dbh->prepare($query);

        public function bind($param, $value, $type = null){
                    case is_int($value):
                        $type = PDO::PARAM_INT;

                    case is_bool($value):
                        $type = PDO::PARAM_BOOL;

                    case is_null($value):
                        $type = PDO::PARAM_NULL;

                    default : 
                        $type = PDO::PARAM_STR;

            $this->stmt->bindValue($param, $value, $type);

        public function execute(){
            return $this->stmt->execute();

        public function resultset(){
            return $this->stmt->fetchAll(PDO::FETCH_OBJ);

        public function single(){
            return $this->stmt->fetch(PDO::FETCH_OBJ);

        public function rowCount(){
            return $this->stmt->rowCount();

        public function lastInsertId(){
            return $this->dbh->lastInsertId();

        public function beginTransaction(){
            return $this->dbh->beginTransaction();

        public function endTransaction(){
            return $this->bbh->commit();

        public function cancelTransaction(){
            return $this->dbh->rollBack();




You have a typo in public function __contruct(), it should be public function __construct() { (you are missing an s)

Source : Link , Question Author : Still Learning , Answer Author : gmc

Leave a Comment