dszm02606009 2017-07-02 19:12
浏览 19

如何在父类中访问相同的名称方法?

Here is a simplified of my code:

class questions {

    public function index( $one = '', $two = '', $three = '' ) {
        return 'sth';
    }
}


class tags extends questions {

    public function index () {
        return parentClass::index();
    }

}

But my code throws this error:

enter image description here

Does anybody know how can I fix the error?

expected result is printing: sth

  • 写回答

1条回答 默认 最新

  • dqwolwst50489 2017-07-02 19:27
    关注

    If you extend a class and override a method, you must make sure the overridden method has the same "prototype", i.e., it must have the same number of method arguments in the same order. That's why you get the first warning:

    Warning: Declaration of tags::index() should be compatible with questions::index($query_where = '', $query_join = '', $called_from = NULL) in C:\xampp\htdocs\myweb\others\tags.php on line 3

    Second, if you want to call a function with the same name from the parent class, you'll need to use the parent keyword:

    class tags extends questions {
    
        public function index ($query_where = '', $query_join = '', $called_from = NULL) {
            return parent::index($query_where, $query_join, $called_from);
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作