【发布时间】:2020-03-04 22:59:36
【问题描述】:
我有以下代码:
<?php
namespace Assessment;
class Database {
private $server;
private $username;
private $password;
private $database;
public $conn;
public function __construct()
{
$this->server = "localhost";
$this->username = "root";
$this->password = "Museum2013";
$this->database = "rest_test";
$this->conn = mysqli_connect("$this->server","$this->username","$this->password","$this->database");
if(!$this->conn)
{
echo "Database connection failed : " . mysqli_connect_error();;
}
}
}
$database = new Assessment\Database;
?>
当我使用命名空间运行代码时,它不起作用。但是当我从顶部和对象中删除命名空间时,代码可以正常工作。我也想在子类中使用命名空间,但我不知道我的代码有什么问题。
【问题讨论】:
-
用
$database = use Assessment\Database;替换$database = new Assessment\Database; -
这不是错误,但页面没有加载,它说并返回 HTTP 错误 500。
-
查看this,对你有帮助
标签: php class oop namespaces