1

検索しましたが、問題の解決策が見つからないようです。

新しいオブジェクトを開始しようとするたびに、次の$con = new Database();エラーが発生します。

Fatal error: Class 'Database' not found in /customers/c/6/f/rowweb.dk/httpd.www/toolkit/e/includes/php/db/connect.php on line 3

少しトラブルシューティングを行ったところ、データベースに問題なく接続しています。データベース クラスと同じファイル内からオブジェクトを開始しようとしています。

これが私のデータベース接続です:

$con = new Database();

class Database extends mysqli {
    function __construct() {
        parent::__construct('host', 'username', 'pass', 'database');
        if (mysqli_connect_errno()) {
            throw new Exception(mysqli_connect_error(),   
            mysqli_connect_errno());
        } 
    } 
}

何が問題なのか知っている人はいますか?

4

2 に答える 2

1

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

于 2013-09-18T17:18:35.647 に答える