PHP interface Keyword
last modified November 25, 2026
The PHP interface keyword defines a contract that classes can implement. An interface states which methods a class must provide, but not how they work. This makes it possible to replace implementations without changing the code that uses them.
Interfaces are the main tool for polymorphism in PHP: a function can accept any object that implements a given interface, no matter which class it belongs to.
Basic definitions
An interface is a contract: it lists methods that implementing classes must provide, without saying how they work. A class signs the contract with the implements keyword and must define every method with a compatible signature.
Methods declared in an interface are public and have no body. An interface may also declare constants. Since PHP 8.4 it may declare properties with hooks, but it still cannot contain ordinary properties or method implementations.
A class can implement several interfaces, while it can extend only one class. Interfaces can extend other interfaces, including several at once. An interface cannot be instantiated; it is used as a type for parameters, return values, and properties.
The main difference from an abstract class is purpose and flexibility. An abstract class can hold state and shared implementation, but a class can inherit from only one of them. Interfaces describe a capability that unrelated classes can share.
A first interface
The first example defines a Greeter contract and one implementation. The calling code depends only on the interface, so another class can replace EnglishGreeter without changing the call.
<?php
declare(strict_types=1);
interface Greeter
{
public function greet(): string;
}
class EnglishGreeter implements Greeter
{
public function greet(): string
{
return "Hello there!";
}
}
$greeter = new EnglishGreeter();
echo $greeter->greet(), "\n";
EnglishGreeter promises to return a string from greet. The variable could be typed as Greeter, and any other class implementing the interface could be assigned to it; this is polymorphism through an interface. If a class forgets a method or changes its signature incompatibly, PHP reports a fatal error when the class is loaded.
$ php greeter.php Hello there!
Implementing multiple interfaces
A class may implement more than one interface. The interfaces are separated by commas after implements. This lets a class play several independent roles without a deep inheritance hierarchy.
<?php
declare(strict_types=1);
interface Renderable
{
public function render(): string;
}
interface Cacheable
{
public function cacheKey(): string;
}
class Page implements Renderable, Cacheable
{
private string $title;
public function __construct(string $title)
{
$this->title = $title;
}
public function render(): string
{
return "<h1>{$this->title}</h1>";
}
public function cacheKey(): string
{
return "page:" . md5($this->title);
}
}
$page = new Page("Welcome");
echo $page->render(), "\n";
echo $page->cacheKey(), "\n";
The Page class must implement all methods from both Renderable and Cacheable. The two contracts are independent: another class could implement only Renderable, and code that renders pages would still accept it. The cache key uses md5 only to produce a short deterministic string in this example; it is not a security measure.
$ php multiple.php <h1>Welcome</h1> page:83218ac34c1834c26781fe4bde918ee4
Extending interfaces
An interface can extend another interface and add more methods. Unlike classes, an interface may extend several interfaces at once.
<?php
declare(strict_types=1);
interface Vehicle
{
public function start(): void;
public function stop(): void;
}
interface Named
{
public function name(): string;
}
interface Car extends Vehicle, Named
{
public function accelerate(float $speed): void;
}
class Sedan implements Car
{
public function start(): void
{
echo "Engine started\n";
}
public function stop(): void
{
echo "Engine stopped\n";
}
public function name(): string
{
return "Sedan";
}
public function accelerate(float $speed): void
{
echo "Accelerating to $speed km/h\n";
}
}
$car = new Sedan();
$car->start();
$car->accelerate(60);
echo $car->name(), "\n";
$car->stop();
Car extends both Vehicle and Named, so Sedan has to implement four methods in total. A variable typed as Vehicle can hold a Sedan, but it exposes only start and stop; the additional methods become available only through the narrower type.
$ php inheritance.php Engine started Accelerating to 60 km/h Sedan Engine stopped
Interface constants
Interfaces may declare constants. They are public, and since PHP 8.1 an implementing class may override a constant unless it is marked final. A final interface constant cannot be overridden.
<?php
declare(strict_types=1);
interface HttpStatus
{
public const OK = 200;
final public const NOT_FOUND = 404;
public function code(): int;
}
class ApiResponse implements HttpStatus
{
// allowed: the constant is not final
public const OK = 201;
public function code(): int
{
return self::OK;
}
}
echo ApiResponse::OK, "\n";
echo HttpStatus::OK, "\n";
echo HttpStatus::NOT_FOUND, "\n";
echo (new ApiResponse())->code(), "\n";
ApiResponse::OK is 201, while HttpStatus::OK still refers to 200. The final NOT_FOUND constant can be read through either name but not changed. Constants are useful for values that belong to the contract itself, such as status codes or format names.
$ php constants.php 201 200 404 201
Type hinting against an interface
The most common use of an interface is a parameter type. The class below depends on Notifier, not on a particular delivery channel, so the channel can be replaced without editing the service.
<?php
declare(strict_types=1);
interface Notifier
{
public function send(string $message): void;
}
class EmailNotifier implements Notifier
{
public function send(string $message): void
{
echo "Email: $message\n";
}
}
class SmsNotifier implements Notifier
{
public function send(string $message): void
{
echo "SMS: $message\n";
}
}
class NotificationService
{
private Notifier $notifier;
public function __construct(Notifier $notifier)
{
$this->notifier = $notifier;
}
public function notify(string $message): void
{
$this->notifier->send($message);
}
}
$service = new NotificationService(new SmsNotifier());
$service->notify("Your order has shipped");
The constructor stores the dependency in a private property and the notify method calls only the interface method. Passing an object that does not implement Notifier raises a TypeError. This is the basis of dependency injection and makes the service easy to test with a fake notifier.
$ php type_hinting.php SMS: Your order has shipped
Interface versus abstract class
An interface and an abstract class can both declare methods that subclasses must implement. The difference is that an abstract class may also contain state and working methods, and a class can extend only one parent.
<?php
declare(strict_types=1);
interface Drawable
{
public function draw(): void;
}
abstract class Shape
{
protected string $color;
public function __construct(string $color)
{
$this->color = $color;
}
abstract public function area(): float;
}
class Circle extends Shape implements Drawable
{
private float $radius;
public function __construct(string $color, float $radius)
{
parent::__construct($color);
$this->radius = $radius;
}
public function draw(): void
{
echo "Drawing a {$this->color} circle\n";
}
public function area(): float
{
return pi() * $this->radius ** 2;
}
}
$circle = new Circle("red", 5);
$circle->draw();
echo "Area: ", round($circle->area(), 2), "\n";
Circle extends Shape and implements Drawable. The abstract class supplies the $color property and its constructor, while the interface adds a separate drawing capability. Use an abstract class when implementations share code and state; use an interface when unrelated classes must expose the same operation.
$ php abstract_vs_interface.php Drawing a red circle Area: 78.54
Built-in interfaces
PHP and its standard library define many interfaces. Countable, for example, lets an object decide what count() means for it. IteratorAggregate makes an object usable in foreach, and Stringable is added automatically to classes that define __toString.
<?php
declare(strict_types=1);
class Cart implements Countable
{
private array $items = [];
public function add(string $item): void
{
$this->items[] = $item;
}
public function count(): int
{
return count($this->items);
}
}
$cart = new Cart();
$cart->add("apple");
$cart->add("bread");
$cart->add("coffee");
echo "Items: ", count($cart), "\n";
echo "Empty: ", var_export(count(new Cart()) === 0, true), "\n";
The count method satisfies the Countable contract, so the global count function works with a Cart object. Without the interface, count($cart) would report 1 for an object, which is rarely what the programmer means. The same pattern is used by frameworks for collections, sessions, and paginators.
$ php countable.php Items: 3 Empty: true
Rules and limitations
The following rules apply when declaring and implementing interfaces.
- An interface cannot be instantiated with
new; only a class that implements it can be created. - Interface methods are public. Declaring one as
protectedorprivateis a fatal error. - An implementing method must be compatible: it may widen parameter types and narrow return types, but it cannot require more specific parameters or promise a broader result.
- An interface may declare static methods; the implementing class must define them as static too.
- Interfaces may contain constants. Ordinary properties are not allowed before PHP 8.4; property hooks are a separate feature.
- An interface may extend several interfaces, and a class may implement several interfaces.
Best practices
Keep interfaces small and focused on one capability. A class with many unrelated methods is hard to implement and hard to fake in tests.
- Single responsibility: prefer several small interfaces over one large one.
- Naming: describe the capability, often with an
-ableor-ersuffix, such asCacheableorNotifier. - Depend on abstractions: type parameters against the interface, not the implementation.
- Document the contract: explain expected behaviour, exceptions, and side effects in the interface docblock.
- Avoid markers: an empty interface documents nothing; use an attribute or a real method instead.
Source
PHP manual: interfaces PHP manual: abstract classes PHP manual: Countable PHP manual: object interfaces
In this article we have used the PHP interface keyword to
define contracts, implement several interfaces, extend interfaces, and type
parameters against them. See also the
PHP interfaces tutorial, which covers
polymorphism in more depth, and the
abstract keyword and
implements keyword articles.
Author
List all PHP tutorials.