我有兩個文件dbconnect.php和config.php文件如何使用另一個文件中的PHP類
dbconnect.php
<?php
class connect{
public function __construct(){
$config = require_once __DIR__ . 'config.php';
}
private $dbhost = $config['host'];
private $dbuser = $config['username'];
private $dbpass = $config['pass'];
private $dbname = $config['dbname'];
public function startConn(){
$this->DBcon = null;
try{
$this->DBcon = new PDO("mysql:host=".$this->dbhost.";dbname=".$this->dbname, $this->dbuser, $this->dbpass);
}catch(Exception $e){
echo "error connecting:";
}
return $this->DBcon;
}
}
?>
的config.php
內<?php
/**
* Contains all configurations
*
*/
return [
'dbname' => 'user',
'pass' => '@user.intern1',
'username' => 'user1',
'host' => 'localhost',
];
?>
在我的dbconnect.php文件中;
如何從我的config.php中包含變量到類中連接
如果我按照以下方式執行操作:
它大叫我,並給我致命錯誤:
「解析錯誤:語法錯誤,意外的'$ config'(T_VARIABLE)在C:\ xampp \ htdocs \ hngfun \ profile \ adeojoemmanuel \ php-handler \ dbconfig.php第8" 行
最重要的未示出的是是'代碼dbconfig.php'像'私人$ dbhost'不能分配依賴於運行時的數據值,如'$配置[」 – user10089632
聲明的屬性主機'];' –
文件'dbconfig.php'在哪裏? –