Heim >php教程 >PHP源码 >Fatal error: Access level to xxx must be protected 错误解决

Fatal error: Access level to xxx must be protected 错误解决

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-08 17:20:023901Durchsuche

php出现Fatal error: Access level to xxx must be protected 错误我们可如何来解决呢,对于这个问题下面我们就一起来看看它的解决办法.

<script>ec(2);</script>

今天程序突然出现这样的报错
Fatal error: Access level to xxx must be protected (as in class xxx) or weaker in xxx.php on line

原因是子类中定义了与父类一样的方法名

子类:
private function return_json($message,$result='true') {
        $data = array();
        $data['result'] = $result;
        $data['message'] = $message;
        self::echo_json($data);
}

private function echo_json($data) {
    if (strtoupper(CHARSET) == 'GBK'){
        $data = Language::getUTF8($data);//网站GBK使用编码时,转换为UTF-8,防止json输出汉字问题
    }
    echo json_encode($data);
}

父类:

/**
 * 返回json状态
 */
protected function return_json($message,$result='true') {
    $data = array();
    $data['result'] = $result;
    $data['message'] = $message;
    self::echo_json($data);
}

protected function echo_json($data) {
    if (strtoupper(CHARSET) == 'GBK'){
        $data = Language::getUTF8($data);//网站GBK使用编码时,转换为UTF-8,防止json输出汉字问题
    }
    echo json_encode($data);die;
}

解决办法

把子类中的private 改成 protected,或者避免方法重名。这里例子中很显然是相同的方法进行了重复的定义,子类删除这两个方法即可。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn