title: PHP Database Class query Fatal error
body: <p>Folks,</p>
  
  <p>Let me preface this by stating that I'm trying to learn OOP so rather than use one of the existing Mysqli abstraction layers out there, I wanted to write my own Class.</p>
  
  <p>I have the class defined in a file, and I'm just trying to get a generic query working before I start writing specific query functions.</p>
  
  <pre><code>&lt;?php
  
  /* Database Config */
  define('DB_NAME', 'project');
  define('DB_USER', 'foo');
  define('DB_PASS', 'bar');
  define('DB_HOSTNAME', '127.0.0.1');
  
  
  class Database {
  
    private $host;
    private $user;
    private $pw;
    private $db;
    public $con;
  
    public function __construct($hostname, $user, $pass, $db) {
      $this-&gt;DB_HOSTNAME = $hostname; 
      $this-&gt;DB_USER = $user;
      $this-&gt;DB_PASS = $pass;
      $this-&gt;DB_NAME = $db;
  
    }
  
     public function connect() {
          $this-&gt;con = new mysqli($this-&gt;hostname, $this-&gt;user, $this-&gt;pass, $this-&gt;db)
              or die('Database unavailable');
          $this-&gt;con-&gt;set_charset ('utf8');
      } 
  
      public function query($sql) {
          $stmt = $this-&gt;con-&gt;query($sql);
      }
  
  }
  
  // Create a single global instance of the database class
  global $DB;
  $DB = new Database();
  ?&gt;
  </code></pre>
  
  <p>Then I created a simple test.php containing:</p>
  
  <pre><code>&lt;?php
  
  include_once("include/config.inc");
  
  if ($DB) {
  $sql = "select user_login, user_name from users where 1 order by user_name, user_login";
  $stmt = $DB-&gt;query($sql) or die($DB-&gt;errno.__LINE__);
  $result = $stmt-&gt;fetch_array(MYSQLI_ASSOC);
  
  var_dump($result);
  }
  else die('Database Unavailable');
  
  ?&gt;
  </code></pre>
  
  <p>When I call that page I get</p>
  
  <p><strong>Fatal error: Call to a member function query() on a non-object in /www/htdocs/project/include/db.class.php on line 33</strong></p>
  
  <p>I'm not getting a die from the test page, so I believe it can reach the database, and I know the table holds data and the query works because if I switch to non Class based DB definition I get an array with expected data.</p>
  
  <p>So I'm pretty sure it's a problem I created in the Class, I just can't figure out what it is.  Advice please?</p>