title: Call to a member function on null?
body: <p>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.</p>
  
  <p>Below is class called <code>Topic</code> :</p>
  
  <pre><code>class Topic {
  
          // Init DB variables
          private $db;
  
          /*
          *   Constructor
          */
          public function __contruct() {
              $this-&gt;db = new Database();
  
          }
  
          /*
          *   Get All Topics
          */
          public function getAllTopics() {
  
              $this-&gt;db-&gt;query("SELECT topics.*, users.username, 
                                users.avatar, categories.name 
                                FROM topics 
                                INNER JOIN users 
                                ON topics.user_id = users.id 
                                INNER JOIN categories 
                                ON topics.category_id = categories.id 
                                ORDER BY create_date DESC");
  
  
              // Assign the results
              $results = $this-&gt;db-&gt;resultset();
  
              return $results;
          }
      }
  </code></pre>
  
  <p>When I create an object of class <code>Topic</code>, </p>
  
  <pre><code>$topic = new Topic();
  $results = $topic-&gt;getAllTopics();
  </code></pre>
  
  <p>there is always an error like this :</p>
  
  <blockquote>
    <p><strong>Fatal error</strong>: Call to a member function query() on null in
    /Applications/XAMPP/xamppfiles/htdocs/forumproject/libraries/Topic.php
    on line 25 i.e. $this->db->query(...)</p>
  </blockquote>
  
  <p>The <code>Database</code> class is working perfectly as I have run tests independently for that class, and there is no problem with it.</p>
  
  <p>Here is the Database class :</p>
  
  <pre><code>&lt;?php 
  
      /*
      * 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-&gt;host . ';dbname=' . $this-&gt;dbname;
  
              // Set options
              $options = array(
                  PDO::ATTR_PERSISTENT =&gt; true, 
                  PDO::ATTR_ERRMODE =&gt; PDO::ERRMODE_EXCEPTION
              );
  
              // Create a new PDO instance
              try { 
                  $this-&gt;dbh = new PDO($dsn, $this-&gt;user, $this-&gt;pass, $options);
              }
              // Catch the errors using exceptions handler
              catch(PDOException $e){
                  $this-&gt;error = $e-&gt;getMessage();
              }
          }
  
          public function query($query){
              $this-&gt;stmt = $this-&gt;dbh-&gt;prepare($query);
          }
  
          public function bind($param, $value, $type = null){
              if(isnull($type)){
                  switch(true){
                      case is_int($value):
                          $type = PDO::PARAM_INT;
                          break;
  
                      case is_bool($value):
                          $type = PDO::PARAM_BOOL;
                          break;
  
                      case is_null($value):
                          $type = PDO::PARAM_NULL;
                          break;
  
                      default : 
                          $type = PDO::PARAM_STR;
                  }
              }
  
              $this-&gt;stmt-&gt;bindValue($param, $value, $type);
          }
  
  
          public function execute(){
              return $this-&gt;stmt-&gt;execute();
          }
  
          public function resultset(){
              $this-&gt;execute();
              return $this-&gt;stmt-&gt;fetchAll(PDO::FETCH_OBJ);
          }
  
          public function single(){
              $this-&gt;execute();
              return $this-&gt;stmt-&gt;fetch(PDO::FETCH_OBJ);
          }
  
          public function rowCount(){
              return $this-&gt;stmt-&gt;rowCount();
          }
  
          public function lastInsertId(){
              return $this-&gt;dbh-&gt;lastInsertId();
          }
  
          public function beginTransaction(){
              return $this-&gt;dbh-&gt;beginTransaction();
          }
  
          public function endTransaction(){
              return $this-&gt;bbh-&gt;commit();
          }
  
          public function cancelTransaction(){
              return $this-&gt;dbh-&gt;rollBack();
          }
  
  
      }
  
  ?&gt;
  </code></pre>