Skip to main content

SOLID principles PHP Edition. Today, Interface Segregation Principle

Jose CerrejonAbout 2 minDeveloper

SOLID principles PHP Edition. Today: Interface Segregation Principle

flying eagles and penguins
Interface Segregation Principle. Generated with AI.

We are almost ending the series of articles about the SOLID principles. Here you have a brief explanation of each principle to review them:


Today we are going to focus on the Interface Segregation Principle. It's very easy to understand, I promise. ❤️

This principle states that a class should never be forced to implement an interface that it doesn't use. If you implement an interface in a class, but only utilize a few of its methods, It indicates that you are violating this principle. An Example:

// Bad
interface Bird {
    public function fly();
    public function swim();
}

class Eagle implements Bird {
    public function fly() {
        return "I can fly";
    }
    public function swim() { <- Forced to implement a method that it does not need
        return "I can't swim";
    }
}

class Penguin implements Bird {
    public function fly() { <- Forced to implement a method that it does not need
        return "I can't fly";
    }
    public function swim() {
        return "I can swim";
    }
}

In this example, the Bird interface has two methods: fly and swim.

The Eagle and the Penguin classes implements both methods, but the Penguin class only should implements the swim method (Have you ever seen a penguin flying?), and Eagle should implement the fly method. This violates the Interface Segregation Principle.

An example with the correct way to implement the Interface Segregation Principle:

// Good
interface Bird {
    public function fly();
}

interface Swimmer {
    public function swim();
}

class Eagle implements Bird {
    public function fly() {
        return "I can fly";
    }
}

class Penguin implements Swimmer {
    public function swim() {
        return "I can swim";
    }
}

Now we have two interfaces: Bird and Swimmer. The Eagle class implements the Bird interface, and the Penguin class implements the Swimmer interface. This way, we are following the principle we are discussing. Easy, right?. 😄

I hope you have enjoyed the article today. If you have any questions or suggestions, in the meantime I add comments to the blog, you can send me an email.

See you in the next article!.