SyntaxStudy
Sign Up
PHP Interface Segregation Principle
PHP Intermediate 5 min read

Interface Segregation Principle

Interface Segregation

The ISP states that a class should not be forced to implement interfaces it does not use. Prefer many small, specific interfaces over one large general one.

Example
<?php
// Bad: one fat interface (ISP violation)
interface Worker {
    public function work(): void;
    public function eat(): void;
    public function sleep(): void;
}

// Good: segregated interfaces
interface Workable { public function work(): void; }
interface Eatable  { public function eat(): void; }
interface Sleepable { public function sleep(): void; }

class HumanWorker implements Workable, Eatable, Sleepable {
    public function work(): void {}
    public function eat(): void {}
    public function sleep(): void {}
}

class Robot implements Workable {
    public function work(): void {} // Robots do not eat or sleep
}
Pro Tip

A good interface should have a cohesive set of related methods — if you need to write stub "not implemented" methods, the interface is too large.