-2

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

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • Check the code in autoloader.php... – Jocelyn Jul 02 '17 at 19:16
  • 2
    Instead of `parentClass::index()` you should use `parent::index()` to call `questions::index()` from the `tags` class. – rickdenhaan Jul 02 '17 at 19:17
  • have u tried return questions::index(); – Leo Jul 02 '17 at 19:17
  • There's no `parentClass` in your code. For accessing parents there's a keyword `parent`. – u_mulder Jul 02 '17 at 19:17
  • @Jocelyn ``. Do you know what's wrong with it? – Martin AJ Jul 02 '17 at 19:18
  • @u_mulder Oh, [this](https://stackoverflow.com/questions/3115388/declaration-of-methods-should-be-compatible-with-parent-methods-in-php#3115398) made me wrong. – Martin AJ Jul 02 '17 at 19:19
  • Possible duplicate of [How do I access a parent's methods inside a child's constructor in PHP?](https://stackoverflow.com/questions/20494518/how-do-i-access-a-parents-methods-inside-a-childs-constructor-in-php) – Jocelyn Jul 02 '17 at 19:35

1 Answers1

2

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);
    }

}
rickdenhaan
  • 10,857
  • 28
  • 37