[英]how to use another file inside a php class
我有两个文件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中的变量包含到类connect中
如果我按照上面的方式做;
它对我大吼,并给我致命错误:
“解析错误:语法错误,在第8行的C:\\ xampp \\ htdocs \\ hngfun \\ profile \\ adeojoemmanuel \\ php-handler \\ dbconfig.php中出现意外的'$ config'(T_VARIABLE)”
我在这里猜。 但是我可以清楚地看到,您正在将$ config设置为构造函数中的局部变量。 这意味着一旦离开构造函数,该函数将不可用。
<?php
class connect{
public function __construct(){
$config = require_once __DIR__ . 'config.php';
$this->dbhost = $config['host'];
$this->dbuser = $config['username'];
$this->dbpass = $config['pass'];
$this->dbname = $config['dbname'];
}
private $dbhost ;
private $dbuser ;
private $dbpass ;
private $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;
}
}
不能为依赖于运行时数据的已声明属性(如private $dbhost
分配值,例如$config['host'];
private $dbhost
引用PHP文档 :
该声明可以包括一个初始化,但是此初始化必须是一个常量值-也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估。
解决方案,在构造函数中分配值:
class connect{
public function __construct(){
$config = require_once __DIR__ . 'config.php';
private $this->dbhost = $config['host'];
private $this->dbuser = $config['username'];
private $this->dbpass = $config['pass'];
private $this->dbname = $config['dbname'];
}
private $dbhost;
private $dbuser;
private $dbpass;
private $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;
}
}
您需要在构造函数内部进行设置:
private $dbhost;
private $dbuser;
private $dbpass;
private $dbname;
public function __construct(){
$config = require_once __DIR__ . 'config.php';
$this->dbhost = $config['host'];
$this->dbuser = $config['username'];
$this->dbpass = $config['pass'];
$this->dbname = $config['dbname'];
}
第一个问题是您不能只从config.php文件返回任何内容。 您只能在函数内返回结果。 一种实现方法是将数组声明为全局变量,然后在需要该配置数组的所有其他php文件中使用它。
<?php
/**
* Contains all configurations
*
*/
$dbconfig = array(
'dbname' => 'user',
'pass' => '@user.intern1',
'username' => 'user1',
'host' => 'localhost',
);
?>
<?php
require_once __DIR__ . 'config.php';
class connect{
public function __construct(){
}
private $dbhost = $dbconfig['host'];
private $dbuser = $dbconfig['username'];
private $dbpass = $dbconfig['pass'];
private $dbname = $dbconfig['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;
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.