W3docs

implements

Learn how PHP's "implements" keyword enforces interface contracts. Covers syntax, multiple interfaces, constants, type hinting, and common errors.

The PHP implements Keyword

The implements keyword tells PHP that a class agrees to fulfil the contract defined by an interface. An interface lists method signatures (and optionally constants) but no method bodies; a class that implements it must provide a concrete body for every method the interface declares. If it doesn't, PHP throws a fatal error before the program runs.

This page covers the syntax, implementing several interfaces at once, interface constants, type hinting against interfaces, the common errors you'll hit, and how implements differs from extends. If interfaces themselves are new to you, start with PHP Interfaces and What is OOP.

Syntax

interface MyInterface {
  public function doSomething(); // signature only — no body
}

class MyClass implements MyInterface {
  public function doSomething() {
    // the concrete implementation lives here
  }
}

MyClass implements MyInterface is a promise: "this class provides a working version of every method MyInterface requires." The interface is the what; the class is the how.

A basic example

<?php

interface Animal {
  public function makeSound();
}

class Dog implements Animal {
  public function makeSound() {
    echo "Woof!";
  }
}

class Cat implements Animal {
  public function makeSound() {
    echo "Meow!";
  }
}

$animals = [new Dog(), new Cat()];
foreach ($animals as $animal) {
  $animal->makeSound(); // Output: Woof!Meow!
}

Because both Dog and Cat implement Animal, any code that expects an Animal can work with either one without knowing the concrete class. This is the foundation of polymorphism in PHP.

Implementing multiple interfaces

Unlike extends (a class can extend only one parent), a class can implement any number of interfaces — separate them with commas. The class must satisfy every method from every interface it lists.

<?php

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

interface Notifier {
  public function notify(string $message): void;
}

class AlertService implements Logger, Notifier {
  public function log(string $message): void {
    echo "LOG: $message\n";
  }
  public function notify(string $message): void {
    echo "NOTIFY: $message\n";
  }
}

$service = new AlertService();
$service->log("Disk space low");    // Output: LOG: Disk space low
$service->notify("Disk space low"); // Output: NOTIFY: Disk space low

This is how PHP gets the benefits of multiple inheritance (sharing several contracts) without its downsides (ambiguous inherited implementations).

Interface constants and type hinting

An interface can declare constants, and an implementing class accesses them like its own. The real power, though, is type hinting: when you type a parameter against the interface, any implementing class is accepted — so you can swap implementations freely.

<?php

interface PaymentGateway {
  const CURRENCY = "USD";
  public function charge(float $amount): bool;
}

class StripeGateway implements PaymentGateway {
  public function charge(float $amount): bool {
    echo "Charging " . self::CURRENCY . " $amount\n";
    return true;
  }
}

// Accepts ANY PaymentGateway, not just StripeGateway
function processPayment(PaymentGateway $gateway, float $amount): void {
  $gateway->charge($amount);
}

processPayment(new StripeGateway(), 49.99); // Output: Charging USD 49.99

You can also test an object against an interface at runtime with instanceof:

var_dump($gateway instanceof PaymentGateway); // bool(true)

Common errors and gotchas

  • Missing a method is fatal. If a class skips even one interface method, PHP throws Fatal error: Class X contains 1 abstract method and must therefore be declared abstract or implement the remaining methods. The check happens at compile time, before any code runs.
  • Signatures must be compatible. Your implementation must keep the parameter and return types declared by the interface (you may widen parameter types and narrow return types under PHP's variance rules, but mismatches are fatal).
  • Interface methods are implicitly public. You cannot implement an interface method as protected or private.
  • Interfaces can extend interfaces. Use interface B extends A to build on another interface; a class implementing B must satisfy methods from both. Note this uses extends, not implements.

implements vs extends

These keywords are easy to confuse:

extendsimplements
Used withone parent class (or interface→interface)one or more interfaces
Inherits code?yes — properties and method bodiesno — only the contract (signatures)
How many?a class extends one classa class implements many interfaces

A class can do both at once: class Circle extends Shape implements JsonSerializable { ... }. For inheritance details see PHP extends and, when you want partial implementations, PHP abstract classes.

When to use implements

Reach for an interface and implements when:

  • Several unrelated classes need to be interchangeable (e.g. multiple payment gateways, loggers, or cache drivers).
  • You want to type hint against a capability rather than a concrete class, keeping code loosely coupled and easy to test with mocks.
  • You're defining a public API contract that other classes — including ones written later or by other teams — must honour.

If you instead need to share actual implementation code among related classes, use class inheritance (extends) or an abstract class.

Practice

Practice
What is the function of the 'implements' keyword in PHP?
What is the function of the 'implements' keyword in PHP?
Was this page helpful?