Extending interfaces

Permanent Link: Extending interfaces 21. Januar 2009 Comment No Comment

What many people don't know is that you cannot only extend classes in PHP but also interfaces:

interface First_Interface
{
public function show();
}

interface Second_Interface extends First_Interface
{
public function update();
}

class Product implements Second_Interface
{
public function show() {}
public function update() {}
}

You have to remember though that extending is not possible when you have the same function in both interfaces:

interface Second_Interface extends First_Interface
{
    public function show();
    public function update();
}

This will lead to a PHP Fatal Error:
PHP Fatal error:  Can't inherit abstract function First_Interface::show() (previously declared abstract in Second_Interface) in /tmp/interfaces.php on line 8

Fatal error: Can't inherit abstract function First_Interface::show() (previously declared abstract in Second_Interface) in /tmp/interfaces.php on line 8

Note: This possibility is also described in the PHP documentation for interfaces (Example #3)