title: Call to a member function on a non-object with mysqli
body: <p>I'm starting to learn how to use OOP within PHP and so far I want to create a Database class that looks like this:</p>
  
  <pre><code>class Database{
    //Database connection variables
    private $DBHost = "localhost";
    private $DBUser = "username";
    private $DBPass = "password";
    private $DBName = "database3";
    public $DBCon;
  
    public function __construct(){
      $this-&gt;DBCon = new mysqli($this-&gt;DBHost,$this-&gt;DBUser,$this-&gt;DBPass,$this-&gt;DBName);
    }
  
    public function con(){
      return $this-&gt;DBCon;
    }
  
    public function __destruct(){
      $this-&gt;DBCon-&gt;close();
    }
  }
  </code></pre>
  
  <p>And I'm trying to interact with that class from another one called Application:</p>
  
  <pre><code>include('Database.php');
  class Application{
    public $DB;
  
    public function __construct() {
      $DB = new Database();
    }
  
    public function InsertName($Username){
    var_dump($this-&gt;DB-&gt;con());
      if($this-&gt;DB-&gt;con()-&gt;query("INSERT INTO Test (name) VALUES ($Username);") === TRUE){
        echo "Okay";
      }else{
        echo "Error";
      }
    }
  }
  </code></pre>
  
  <p>But I get the error <code>Call to a member function con() on a non-object</code></p>
  
  <p>As a side note, is this an appropriate way to interact with a database in OOP?</p>