我正在尝试使用将主机等作为变量的 php 创建一个 db 类。我无法坚持初始化值,我不知道为什么。当我在将它们设置为公共的顶部初始化它们时,它工作正常,但是当我尝试在构造函数中初始化它们时它不起作用。
class Database {
public $dbHost;
public $dbUser;
public $dbPass;
public $dbName;
public $db;
public function __construct($Host, $User, $Pass, $Name){
$dbHost = $Host;
$dbUser = $User;
$dbPass = $Pass;
$dbName = $Name;
$this->dbConnect();
}
public function dbConnect(){
echo $dbPass;
$this->db = new mysqli($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
/* check connection */
if (mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}else{
//echo 'connection made';
}
}