Unless you have some other code that is not shown here, your problem is that you have not included the file which defines the Database object.
There are a couple of ways to accomplish this, either by using include/include_once
or require/require_once
, or by using an autoloader.
Include & Require
This method of inclusion will, in effect, join the included file to the current file. For example, if you have a file called a.php
like this:
$varA = 'I am A!';
and a file called b.php
like this:
include('a.php');
echo $varA;
...you can write code in b.php
as though the code in a.php were in the same file. The alternative to include
, require
, works exactly the same, with one important difference. If a file that is include
'd cannot be found, a warning will be issued. If a file that is require
'd cannot be found, it will be a fatal error.
Both flavors have a *_once
variation. Without it, the file is appended each time, whereas include_once
will only append the code the first time it is called on a given file.
Autoloader
There are two ways to add an autoloader. An autoloader is a "helper" that guides your script to the correct location of a file. This code is executed whenever you attempt to create a new class using the new
keyword, or in certain other circumstances.
One method of autoloading uses the magic function __autoload. As a general rule, don't use this. The alternative is spl_autoload_register
[docs], which registers a given function as a "helper". Unless you have a specific reason not to use this approach, you should.
Here is a simple example, right from the documentation:
function my_autoloader($class) {
include 'classes/' . $class . '.class.php';
}
spl_autoload_register('my_autoloader');
Documentation