2017-07-02 91 views
-2

下面是一个简化我的代码:如何在父类中访问相同的名称方法?

class questions { 

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


class tags extends questions { 

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

} 

但我的代码抛出这个错误:

enter image description here

是否有人知道我可以修正这个错误?

expected result is printing: sth

+0

检查在autoloader.php中的代码... – Jocelyn

+2

您应该使用'parent :: index()'从'tags'类调用'questions :: index()'而不是'parentClass :: index()'。 – rickdenhaan

+0

你试过回答问题:: index(); –

回答

2

如果要扩展一个类并重写一个方法,你必须确保重载方法具有相同的“原型”,即它必须以相同的顺序相同数量的方法参数。这就是为什么你得到的第一个警告:

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

第二,如果你想调用与父类同名的功能,你需要使用parent关键字:

class tags extends questions { 

    public function index ($query_where = '', $query_join = '', $called_from = NULL) { 
     return parent::index($query_where, $query_join, $called_from); 
    } 

} 
相关问题