Php 同一类中的两个函数不能相互访问
我目前正在学习面向对象的PHP,因为我总是使用过程化的,我认为是时候了。长话短说,我总是会遇到以下错误:调用未定义函数当我试图调用同一类中的函数时,它不会工作。代码如下:Php 同一类中的两个函数不能相互访问,php,function,scope,Php,Function,Scope,我目前正在学习面向对象的PHP,因为我总是使用过程化的,我认为是时候了。长话短说,我总是会遇到以下错误:调用未定义函数当我试图调用同一类中的函数时,它不会工作。代码如下: public function getStats( $imgid ) { $imgid = $this->mysqli->real_escape_string($imgid); $result = $this->mysqli->query("SELECT * FROM image
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];
}
这也是在索引中调用函数的部分:
$image = new Image;
if( isset( $_GET['imgid'] ) ) {
$imgStats = $image->getStats( $_GET['imgid'] );
}
我已经尝试过将函数从private更改为public,但这并没有改变任何事情。您是否尝试过在函数之前添加$this->?我假设它在一个类中,我认为您需要引用您正在使用的对象
$this->functionName();
这个答案更详细:$This->getUploaderName$result[1];等等,@Abracadave非常感谢,我太蠢了。