我正在尝试在函数中的mysqli变量上编译if语句,但是我收到一个致命错误,声明`在第117行不在对象上下文时使用$ this。我做错了什么以及如何我可以在函数中使用if语句吗?
代码:
$qandaquery = "SELECT ReplyType
FROM Reply";
$qandaqrystmt=$mysqli->prepare($qandaquery);
// get result and assign variables (prefix with db)
$qandaqrystmt->execute();
$qandaqrystmt->bind_result($qandaReplyType);
$this->shared_result[] = $qandaReplyType; //ERROR HERE
function ExpandOptionType($option) {
if('Single' == $this->shared_result){
...
}else if('Multiple' == $this->shared_result){
...
}
}
答案 0 :(得分:1)
您正在尝试在通用函数(不是方法)中使用$this
。它应该写成以下内容:
class A {
private $shared_result = array();
public function __construct($mysqli) {
$qandaquery = "SELECT ReplyType FROM Reply";
$qandaqrystmt = $mysqli->prepare($qandaquery);
$qandaqrystmt->execute();
$qandaqrystmt->bind_result($qandaReplyType);
$this->shared_result[] = $qandaReplyType;
}
public function ExpandOptionType($option) {
if('Single' == $this->shared_result){
...
} else if('Multiple' == $this->shared_result) {
...
}
}
}
有意义。请记住,$this
始终引用您正在使用的类的实例。在非类上下文中它只是没有意义。