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条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路