Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does a PHP Extended class inherit the interfaces as well?

This is more a question to ease development, as well as a best practice question.

<?php
class FOO implements Iterator
{
    ....
}

class BAR extends FOO
{
    ....
}

class OTHER extends FOO implements Iterator
{
    ....
}
?>

In the above example, would BAR also have the Iterator interface? If so, is it better to define the interface every time anyhow (like OTHER does) or would the BAR definition be considered best practice?

I have simplified this example, as we are using an Abstract Class for FOO, that we want to ensure has Iterator support, which may then be overridden in the BAR class for moving between elements. We want some functions to be forced to be defined for the subclasses, but want to inherit the other methods as appropriate, and still use the basic iterators, without having to define the interface on the child classes.

like image 668
Steven Scott Avatar asked Jan 09 '13 17:01

Steven Scott


1 Answers

The subclass will inherit the superclass's implementation of the Iterator interface, which it is free to override. You do not need to explicitly say implements Iterator, but if you do then you must implement all of the Interface's methods in the subclass.

like image 94
Madbreaks Avatar answered Sep 19 '22 18:09

Madbreaks