Skip to content

PHP Interfaces Can Extend Multiple Interfaces

May 8, 2017 1 min read #php

This post is more than 18 months old. Technology changes quickly, so some of this content may be out of date (but that's not always the case). Please verify any technical or programming information against the current release.

Every PHP programmer knows you can’t extend multiple classes with PHP. You can only do one - which is fine. In fact, if you need more shared code, make sure to focus on using traits instead.

What I didn’t realize, until recently, is that PHP Interfaces can extend multiple other interfaces, though. (I admit, I haven’t been much of a contract-based programmer until recently.) Let me show you what I mean:

interface Talks
{
  public function say(string $message);
}

interface Eats
{
  public function putInMouth(FoodObject $food);
}

interface Human extends Talks, Eats
{
  public function singsOffKey(SongObject $song);
}
php

Now, when you have an object that implements the Human interface, it’s going to have to define the putInMouth and say methods in addition to the singsOffKey method. Pretty sweet.