dongni1892 2011-06-16 04:46
浏览 77
已采纳

PHP致命错误:在非对象中调用成员函数getScore()

this is my first question

I Have following class

class ProDetection {
public function ProDetection ( ) { }
public function detect($word) {
    ............
}
public function getScore() {
    return $score;
}
}

class SaveDetection {
public function SaveDetection($words) {
    $proDetection = new ProDetection();
    for($i=0;$i<sizeof($words);$i++) {
        $proDetection->detect($words[$i]);
    }
}
public function getScore() {
    return $this->proDetection->getScore();\\line 22
}
}

in other PHP file, i'm try to calling SaveDetection to getScore();

$konten = "xxxx xxxx xxx";
$save = new SaveDetection($konten);
print_r( $save->getScore() );

But i got an error message

Notice: Undefined property: SaveDetection::$proDetection in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22

Fatal error: Call to a member function getScore() on a non-object in C:\xampp\htdocs\inovasi\SaveDetection.php on line 22

Please need your help

  • 写回答

2条回答 默认 最新

  • duanbaque4230 2011-06-16 04:56
    关注

    Try this. Declare your variables as private (Coz if your code grows, it is hard to find what all the object or variables are used in the class.)

    class SaveDetection {
        private $proDetection = null;
        public function __construct($words) {
            $this->proDetection = new ProDetection();
            for($i=0;$i<sizeof($words);$i++) {
                $this->proDetection->detect($words[$i]);
            }
        }
        public function getScore() {
            return $this->proDetection->getScore();\\line 22
        }
    
        private $proDetection;
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?