【发布时间】:2017-08-24 21:54:04
【问题描述】:
我有两个文件 dbconnect.php 和 config.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 中的变量包含到连接类中
如果我按照上面的方式进行操作;
它对我大喊大叫并给我致命错误:
“解析错误:语法错误,第 8 行 C:\xampp\htdocs\hngfun\profile\adeojoemmanuel\php-handler\dbconfig.php 中的意外 '$config' (T_VARIABLE)”
【问题讨论】:
-
最重要的是
dbconfig.php的代码没有显示 -
不能为
private $dbhost等声明的属性分配依赖于运行时数据的值,例如$config['host']; -
文件
dbconfig.php在哪里?