我现在正在学习面向对象的PHP,因为我一直使用过程式程序,而且我认为这是时候了。长话短说,当我尝试调用同一类中的函数时,总是会收到以下错误“未捕获的错误:调用未定义的函数”,它将无法正常工作。这里的代码:
GROUP_ID ISO_CALENDAR_WEEK PROMOTION_DESCRIPTION MEDIA_SUPPORT
40315 201727 Voordeelzakken En Dozen Online Campagne
40284 201726 Gehwol / Wartner 2e halve prijs Radio
40315 201726 Voordeelzakken En Dozen Online Campagne
3 rows selected.
这也是在索引中调用函数的部分:
public function getStats( $imgid ) {
$imgid = $this->mysqli->real_escape_string($imgid);
$result = $this->mysqli->query("SELECT * FROM images WHERE imgid = " . $imgid);
if( mysqli_num_rows( $result ) != 1 ) {
print("Image not found!");
exit();
}
$result = $result->fetch_array();
$returnVal = array();
$uploader = getUploaderName($result[1]);
$upvotes = getVotes($imgid, '0');
$downvotes = getVotes($imgid, '1');
array_push($returnVal, $result[0], $uploader, $result[2], $upvotes, $downvotes, $result[3], $result[4]);
return $returnVal;
}
private function getUploaderName( $uid ) {
$result = $this->mysqli->query("SELECT name FROM users WHERE uid = " . $uid);
$result = $result->fetch_array();
return $result[0];
}
private function getVotes( $imgid, $type ) {
# If $type == 0 => Get Upvotes; If $type == 1 => Get Downvotes
$result = $this->mysqli->query("SELECT COUNT(vid) FROM votes_i WHERE imgid = " . $imgid . " AND type = '" . $type . "'");
$result = $result->fetch_array();
return $result[0];
}
我已经尝试将功能从私有更改为公共,但这并没有改变。
答案 0 :(得分:0)
您是否曾尝试在函数之前添加$ this->?我假设它在一个类中,并且我认为您需要引用正在使用的对象。
$this->functionName();
此答案有更多详细信息:https://stackoverflow.com/a/1523484/9186388