PHP Interfaces – Structure, Implementation & Best Practices

Interfaces in PHP provide a formal contract that defines what methods a class must implement. While they don’t contain method bodies, they enforce structure and are essential for achieving polymorphism and building scalable, maintainable applications in object-oriented PHP.

This guide will help you understand what PHP interfaces are, how they work, and when to use them effectively.


What is an Interface in PHP?

An interface in PHP is a structure that defines a list of methods that a class must implement—without providing their implementation.

interface Logger {
    public function log($message);
}

Think of interfaces as blueprints for classes.


Why Use Interfaces?

Enforce a consistent API across different classes
Achieve polymorphism (different objects with the same interface)
Improve code flexibility and testability
Enable dependency injection and mocking in unit tests


Defining an Interface

Use the interface keyword. All methods are public and have no body.

interface Shape {
    public function area();
    public function perimeter();
}
  • Cannot define properties
  • Cannot contain method bodies
  • Cannot be instantiated

Implementing Interfaces

A class uses the implements keyword to adhere to the interface contract.

class Circle implements Shape {
    private $radius;

    public function __construct($r) {
        $this->radius = $r;
    }

    public function area() {
        return pi() * $this->radius * $this->radius;
    }

    public function perimeter() {
        return 2 * pi() * $this->radius;
    }
}

If a class fails to implement all interface methods, a fatal error occurs.


Multiple Interfaces

A class can implement multiple interfaces using commas.

interface A {
    public function foo();
}
interface B {
    public function bar();
}

class MyClass implements A, B {
    public function foo() { echo "foo"; }
    public function bar() { echo "bar"; }
}

Interface vs Abstract Class

FeatureInterfaceAbstract Class
MethodsOnly signaturesCan have both
Properties Not allowed Allowed
Multiple Inheritance Yes No
Instantiation Not allowed Not allowed
Use CaseDefine contractProvide base functionality

Use interfaces when you need to enforce a contract.
Use abstract classes when you want to share common logic.


Type Hinting with Interfaces

Interfaces are ideal for dependency injection via type hinting.

class FileLogger implements Logger {
    public function log($message) {
        file_put_contents('log.txt', $message, FILE_APPEND);
    }
}

class App {
    private Logger $logger;

    public function __construct(Logger $logger) {
        $this->logger = $logger;
    }

    public function run() {
        $this->logger->log("App started.\n");
    }
}

You can swap Logger implementations (e.g., FileLogger, DbLogger) without changing the App class.


Interface Inheritance

Interfaces can extend other interfaces.

interface Animal {
    public function eat();
}

interface Pet extends Animal {
    public function play();
}

class Dog implements Pet {
    public function eat() { echo "Dog eats"; }
    public function play() { echo "Dog plays"; }
}

Real-World Use Cases

  • Frameworks: Laravel’s Illuminate\Contracts for services
  • Strategy Pattern: Inject different algorithm classes
  • Mocking: Use interface for mocking in PHPUnit
  • Logging: Implement LoggerInterface for various log systems

Best Practices

Name interfaces clearly (e.g., LoggerInterface)
Favor interfaces over concrete classes for flexibility
Keep interfaces small and focused
Avoid breaking changes (e.g., adding new methods) once in use
Use them to define reusable service contracts


Summary – PHP Interfaces

  • PHP interfaces define a contract that classes must follow.
  • They improve flexibility, maintainability, and testability.
  • Interfaces support polymorphism and multiple inheritance.
  • They are ideal for large systems with interchangeable components.

FAQ – Frequently Asked Questions

Can interfaces have constructors?

No. Interfaces can’t have any implementation, including constructors.

Can a class implement multiple interfaces and extend a class?

Yes. A class can extend one class and implement multiple interfaces.

class Dog extends Animal implements Pet, Friendly {}

Is it good practice to use interfaces in small projects?

If future scalability or testability is a concern—yes. Otherwise, they may be overkill.

How are interfaces different from traits?

Interfaces define what a class must do. Traits define how methods behave and are used for code reuse.


Share Now :
Share

🔌 PHP Interfaces

Or Copy Link

CONTENTS
Scroll to Top