PHP Interfaces Can Extend Multiple Interfaces

May 8, 2017 php
This post is more than 18 months old. Since technology changes too rapidly, this content may be out of date (but that's not always the case). Please remember to verify any technical or programming information with 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);
}

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.

Go to All Posts