0

この質問のタイトルが実際の質問を説明していない場合は申し訳ありません。この質問のタイトルにふさわしい言葉が見つかりませんでした。

私は次のようなデータベースクラスを持っています:

class Database
{
  private $link;
  private $host, $username, $password, $database;

  public function __construct($setup)
  {
    if ($setup == 'default') {
        $this->host        = 'localhost';
        $this->username    = 'root';
        $this->password    = 'root';
        $this->database    = 'infobox_sierraleone';
    }

    if ($setup == 'promo') {
        $this->host        = 'localhost';
        $this->username    = 'root';
        $this->password    = 'root';
        $this->database    = 'infobox';
    }

    $this->link = mysql_connect($this->host, $this->username, $this->password)
        OR die("There was a problem connecting to the database.");

    mysql_select_db($this->database, $this->link)
        OR die("There was a problem selecting the database.");

  }

  public function __destruct() 
  {
        mysql_close($this->link)
            OR die("There was a problem disconnecting from the database.");
  }

}

上記のクラスのさまざまなオブジェクトを別のファイルに作成しました。

include 'conn/database.php';

$db = new Database('default');
$db_promo = new Database('promo');

上記のスクリプトを実行すると、次のメッセージが表示されます。

There was a problem disconnecting from the database.

このメッセージが __destruct() 関数からのものであることに気付きました。しばらく調査しましたが、このメッセージが表示される理由とそれを取り除く方法がまだわかりません。

どんな種類の助けも大歓迎です。

ありがとうございました。

編集:コンストラクターにあった return ステートメントを削除しました。

4

1 に答える 1

0

独自のオブジェクトを破棄していないため、スクリプトが終了すると、オブジェクトは PHP によって破棄されます。次に、PHP は取得したものをすべて破棄しますが、必ずしも正しい順序であるとは限りません。

この場合、PHP は最初に MySQL 接続を強制終了し、その後、他のオブジェクト (ユーザーのものを含む) の破棄を続けているようです。die次に、オブジェクトは、既に切断されているデータベース接続を切断しようとしますが、失敗時にスクリプトを許可しないと、気付かれません。

于 2013-06-29T23:52:39.733 に答える