title: PHP OOP - Uncaught Error: Using $this when not in object context
body: <p>So I have a Database class with a function like this:</p>
  
  <pre><code>public function sql($sql) {
      $result = $this-&gt;connection-&gt;query($sql);
      return $result;
  }
  </code></pre>
  
  <p>But it returns the error:</p>
  
  <blockquote>
    <p>Fatal error: Uncaught Error: Using $this when not in object context</p>
  </blockquote>
  
  <p>But I don't get it why I get this error, I've looked al around the internet, but nobody seems to have this exact problem.</p>
  
  <p>This is my full database class:</p>
  
  <pre><code>namespace Core;
  
  require_once ("../config.php");
  
  class DB {
  
      public $connection;
      public function __construct() {
          $this-&gt;connection();
      }
  
      public function connection() {
          $this-&gt;connection = new \mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
  
          if ($this-&gt;connection-&gt;connect_error) {
              die("Database fout");
          }
      }
  
      public function sql($sql) {
          $result = $this-&gt;connection-&gt;query($sql);
          return $result;
      }
  }
  </code></pre>
  
  <p>Then I call it like so:</p>
  
  <pre><code>DB::sql('SELECT * FROM `users`');
  </code></pre>
  
  <p>But then I get the error. :/</p>