Skip to content

PHP Tutorial Advanced OOP

Interfaces

Lesson 214 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

An interface is a contract. It lists the methods a class must provide, but contains no code. Any class that implements the interface promises to have those methods.

index.php
PHP
<?php
interface Logger
{
    public function log(string $message): void;
}

class FileLogger implements Logger
{
    public function log(string $message): void
    {
        echo "[file] $message\n";
    }
}

class ScreenLogger implements Logger
{
    public function log(string $message): void
    {
        echo "[screen] $message\n";
    }
}

function run(Logger $logger): void
{
    $logger->log("Started");
}

run(new FileLogger());
run(new ScreenLogger());
Output
OUTPUT
[file] Started
[screen] Started

The function run() does not care which logger it gets. It only needs something that can log(). That makes the code flexible: you can swap loggers without changing run().

The rules

  • All interface methods are public and have no body.
  • A class uses implements and must write every method.
  • A class can implement many interfaces (separated by commas).
  • An interface can extend other interfaces.
index.php
PHP
<?php
interface Printable
{
    public function print(): string;
}

interface Savable
{
    public function save(): bool;
}

class Document implements Printable, Savable
{
    public function print(): string
    {
        return "Printing document";
    }

    public function save(): bool
    {
        return true;
    }
}

$d = new Document();

echo $d->print() . "\n";
var_dump($d instanceof Savable);
Output
OUTPUT
Printing document
bool(true)

Interface constants

index.php
PHP
<?php
interface HasVersion
{
    const VERSION = "2.0";
}

class Plugin implements HasVersion {}

echo Plugin::VERSION;
Output
OUTPUT
2.0

Extending an interface

index.php
PHP
<?php
interface Reader
{
    public function read(): string;
}

interface ReaderWriter extends Reader
{
    public function write(string $data): void;
}

echo "ReaderWriter requires read() and write()";
Output
OUTPUT
ReaderWriter requires read() and write()

Interfaces are types

You can use an interface as a parameter type, a return type or a property type. This is the basis of dependency injection (see Dependency Injection).

Built-in interfaces worth knowing

InterfaceGives your class the ability to…
Countablework with count($object)
IteratorAggregate / Iteratorbe used in foreach
ArrayAccessbe used like an array ($obj["key"])
Stringablebe used as a string
JsonSerializablecontrol json_encode()
index.php
PHP
<?php
class Playlist implements Countable
{
    private array $songs = ["A", "B", "C"];

    public function count(): int
    {
        return count($this->songs);
    }
}

echo count(new Playlist());
Output
OUTPUT
3

When to use an interface

  • When several classes do the same job in different ways (loggers, payment gateways, storage).
  • When you want to write code that does not depend on a specific class.
  • When you want to test code by swapping a real service for a fake one.
lightbulb
Name by role
Interfaces are named for what the thing is or does: Logger, PaymentGateway, Cache. Some projects add Interface as a suffix, others do not. Follow your project’s style.