我正在尝试编写一个php论坛的OOP风格,我有一个难度,这是我第一次编写OOP样式并且我一直收到错误
第5行出乎意料的T_VARIABLE
这是我的代码
<?php
class dbManager{
var $MySQL_Conx;
var $details = configManager::getConfig;
var $mysqli = $this->dbConnect();
public function dbSelect($sql, $mysqli){
}
private function dbConnect(){
$this->CloseConnection()
$this->MySQL_Conx = mysqli_connect($this->details['dbhost'], $this->details['dbuser'], $this->details['dbpass'], $this->details['dbdatabase'])
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
} else {
return $MySQL_Conx;
}
}
}
?>
同样configManager::getConfig;
返回一组包含连接数据库的所有细节。
答案 0 :(得分:1)
您需要在constructor
上进行初始化。代码中的注释表明您所犯的错误。
来自PHP手册
这个声明可能包括初始化,但是这个 初始化必须是一个常量值 - 也就是说,它必须能够 在编译时进行评估,不得依赖于运行时 信息以便进行评估。
<?php
class dbManager{
var $MySQL_Conx;
var $details;
var $mysqli;
function __construct() //<---- Wrapped them in a constructor
{
$this->details=configManager::getConfig;
$this->mysqli=$this->dbConnect();
}
public function dbSelect($sql, $mysqli){
}
private function dbConnect(){
$this->CloseConnection(); //<--- You forgot a semicolon
$this->MySQL_Conx = mysqli_connect($this->details['dbhost'], $this->details['dbuser'], $this->details['dbpass'], $this->details['dbdatabase']);
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
} else {
return $this->MySQL_Conx; //<---- Added this keyword
}
}
}
?>
答案 1 :(得分:1)
请在构造函数
中指定值<?php
class dbManager{
var $MySQL_Conx;
var $details = array();
var $mysqli = '';
//you have to assign the values in the constructor
public function __construct()
{
//assign the details
$this->details = configManager::getConfig;
//assign the mysql connection
$this->mysqli = $this->dbConnect()
}
public function dbSelect($sql, $mysqli){
}
private function dbConnect(){
$this->CloseConnection()
$this->MySQL_Conx = mysqli_connect($this->details['dbhost'], $this->details['dbuser'], $this->details['dbpass'], $this->details['dbdatabase']);
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
exit();
} else {
return $MySQL_Conx;
}
}
}