我正在尝试创建一个CMS,而且我在PHP中使用了类。
我有这个小小的连接课,我不知道它为什么不工作。它给了我这个错误:
注意:未定义的变量:C:\ Users \ eha \ Desktop \ Carlos'中的mysqli第4行的Stuff \ xampp \ htdocs \ test \ layout \ system \ class.php
致命错误:无法访问C:\ Users \ eha \ Desktop \ Carlos'中的空属性第4行的Stuff \ xampp \ htdocs \ test \ layout \ system \ class.php
class server_connect {
function __construct($host, $user, $password, $database) {
$this->$mysqli = new mysqli($host, $user, $password, $database);
if ($mysqli->connect_error) {
die('Connect Error (' . $mysqli->connect_errno . ') '
. $mysqli->connect_error);
}
}
protected $mysqli = NULL;
protected function set_connection(&$serverconnect){
$this->$mysqli = $serverconnect;
}
}
class site_manager extends server_connect {
好吧现在我在调用父元素时得到了这个语法$ mysqli我做了父:: $ mysqli并告诉我它是一个未声明的变量
class site_manager extends server_connect {
function __construct(&$conect){
parent::set_connection($conect);
}
public function getContent($page , $sub){
if($page == 'home' || ($page != 'home' && $sub ==NULL))
$qry = "SELECT * FROM public WHERE section = ?";
else
$qry = "SELECT * FROM public WHERE section = ? AND subsection = ?";
$stmt = parent::$mysqli->stmt_init();
答案 0 :(得分:3)
当您尝试访问实例变量时,您需要这样做:
$this->mysqli
你这样做
$this->$mysqli
答案 1 :(得分:2)
你遇到了一些问题:首先,你正在使用$this->$variable
;它应该是$this->variable
。其次,在一行中使用$this->$mysqli
然后在下一行中省略$this->
,写$mysqli
。你需要保留$this->
;在方法范围内,$this->variable
和$variable
是两个不同的变量。
答案 2 :(得分:2)
OOP写法的语法错误。您需要使用$this->mysqli
代替$this->$mysqli
完整的代码修复:
class server_connect {
protected $mysqli = NULL;
function __construct($host, $user, $password, $database) {
$this->mysqli = new mysqli($host, $user, $password, $database);
if ($this->mysqli->connect_error) {
die('Connect Error (' . $this->mysqli->connect_errno . ') '
. $this->mysqli->connect_error);
}
}
protected function set_connection(&$serverconnect){
$this->mysqli = $serverconnect;
}
}
class site_manager extends server_connect {