使用PHP访问类函数时出错

使用PHP访问类函数时出错,php,mysql,Php,Mysql,我面临一个问题。我有一个db config文件,一旦我把这个文件包含到我的php页面中,我就不会得到我的输入数据 创建_user.php: header("Access-Control-Allow-Origin: http://localhost/restapi/"); header("Content-Type: application/x-www-form-urlencoded; charset=UTF-8"); header("Access-Control-Allow-Methods: PO

我面临一个问题。我有一个
db config
文件,一旦我把这个文件包含到我的php页面中,我就不会得到我的输入数据

创建_user.php:

header("Access-Control-Allow-Origin: http://localhost/restapi/");
header("Content-Type: application/x-www-form-urlencoded; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

// files needed to connect to database
include_once 'config/database.php';
include_once 'objects/user.php';
// get database connection
$database = new Database();
$db = $database->getConnection();
echo 'data:::'.$_POST['firstname'];exit;
// instantiate product object
$user = new User($db);
上面是我的php文件,其中包括
数据库
文件

config/database.php:

<?php
// used to get mysql database connection
class Database{
    // specify your own database credentials
    private $host = "localhost";
    private $db_name = "users";
    private $username = "root";
    private $password = "**********";
    public $conn;
    // get the database connection
    public function getConnection(){
        $this->conn = null;
        try{
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
        }catch(PDOException $exception){
            echo "Connection error: " . $exception->getMessage();
        }
        return $this->conn;
    }
}
?>

在包含
数据库
文件并在尝试打印
post参数
时调用该方法后,它将变为空白。我正在使用PHP7。

您的代码绝对正确,我已经测试过了,但您似乎遗漏了文件名中的一个符号。尝试要求一次而不是包括一次


不要在PHP文件的末尾使用?>

删除
exit
这一行
echo'数据::'。$\u POST['firstname';退出
@MasivuyeCokile:已删除,但结果相同。通过打开
error\u reporting
display\u errors
获取错误
致命错误:未捕获错误:在/var/www/html/restapi/api/create\u user.php:16
中未找到类“Database”。类数据库的构造函数在哪里?