我有两个文件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'在哪里? –