对不起,如果这个问题的标题没有解释实际的问题。我找不到合适的词来命名这个问题的标题。
我有一个像这样的数据库类:
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 语句。