title: Yet another 'Call to a member function on a non-object' problem
body: <p>I have a html page that calls a php object to get some data back from the database. It works fine, but the script was getting unwieldy so I decided to break some of it out into a bunch of functions.</p>
  
  <p>So I have the following files:</p>
  
  <pre><code>// htdocs/map.php
  &lt;?php
      include("config.php");
      include("rmap.php");
  
      $id = 1;
      $gamenumber = getGameNumber($id);
      echo $gamenumber;
  ?&gt;
  
  // htdocs/config.php
  &lt;?php
  $_PATHS["base"]      = dirname(dirname(__FILE__)) . "\\";
  $_PATHS["includes"]  = $_PATHS["base"] . "includes\\";
  
  ini_set("include_path", "$_PATHS[includes]");
  ini_set("display_errors", "1");
  error_reporting(E_ALL); 
  
  include("prepend.php");
  ?&gt; 
  
  // includes/prepend.php
  &lt;?php
  include("session.php");
  ?&gt;
  
  // includes/session.php
  &lt;?php
  includes("database.php");
  
  class Session {
      function Session() {
      }
  
  };
  
  $session = new Session;
  ?&gt;
  
  // includes/database.php
  &lt;?php
  include("constants.php");
  
  class Database {
  
      var $connection;
  
      function Database() {
          $this-&gt;connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
          mysql_select_db(DB_NAME, $this-&gt;connection) or die(mysql_error());
      }
  
      function query($query) {
          return mysql_query($query, $this-&gt;connection);
      }
  };
  
  /* Create database connection */
  $database = new Database;
  ?&gt;
  
  // includes/rmap.php
  &lt;?php
  function getGameNumber($id) {
      $query = "SELECT gamenumber FROM account_data WHERE usernum=$id";
      $result = $database-&gt;query($query); // line 5
      return mysql_result($result, 0);
  }
  ?&gt;
  </code></pre>
  
  <p>And constants has a bunch of defines in it (DB_USER, etc).</p>
  
  <p>So, map.php includes config.php. That in turn includes prepend.php which includes session.php and that includes database.php. map.php also includes rmap.php which tries to use the <code>database</code> object.</p>
  
  <p>The problem is I get a </p>
  
  <blockquote>
    <p><strong>Fatal error</strong>:  Call to a member function on a non-object in
    <strong>C:\Develop\map\includes\rmap.php</strong> on line <strong>5</strong></p>
  </blockquote>
  
  <p>The usual cause of this is that the $database object isn't created/initialised, but if I add code to show which gets called when (via <code>echo "DB connection created";</code> and <code>echo "using DB connection";</code>) they are called (or at least displayed) in the correct order.</p>
  
  <p>If I add <code>include("database.php")</code> to rmap.php I get errors about redefining the stuff in constants.php. If I use <code>require_once</code> I get the same errors as before.</p>
  
  <p><strong>What am I doing wrong here?</strong></p>