interface
The "interface" keyword is used in PHP to define a set of methods that a class must implement. In this article, we will explore the syntax and usage of the
The PHP interface Keyword
An interface is a contract: it lists the methods a class must provide, without saying how they work. Any class that implements the interface promises to define every method it declares. This lets you program against a capability ("anything that can be logged", "anything that can be serialized") instead of against a concrete class.
Interfaces cannot be instantiated directly — there is no implementation to run. They exist so that unrelated classes can be treated interchangeably as long as they share the same method signatures.
This page covers how to declare an interface, implement one or several, share constants, extend interfaces, and check types at runtime with instanceof. If you are new to objects in PHP, start with PHP classes and objects and what is OOP.
Syntax
Here is the basic syntax for declaring an interface:
interface MyInterface {
public function someMethod();
}This declares an interface MyInterface with a single method someMethod(). Key rules:
- Method bodies are omitted — an interface only declares signatures, ending each with a semicolon.
- All methods are implicitly public; you may write
publicfor clarity, but no other visibility is allowed (private/protectedare forbidden). - A class adopts an interface with the
implementskeyword and must define every method, or PHP raises a fatal error.
Examples
Let's look at some practical examples of how the interface keyword can be used. The first class implements one interface; the second implements two at once (separated by commas):
<?php
// Example 1
interface MyInterface {
public function someMethod();
}
class MyClass implements MyInterface {
public function someMethod() {
echo "This is from someMethod." . PHP_EOL;
}
}
$obj = new MyClass();
$obj->someMethod();
// Output: This is from someMethod.
// Example 2
interface MyOtherInterface {
public function someOtherMethod();
}
class MyOtherClass implements MyInterface, MyOtherInterface {
public function someMethod() {
echo "This is from someMethod." . PHP_EOL;
}
public function someOtherMethod() {
echo "This is from someOtherMethod." . PHP_EOL;
}
}
$obj2 = new MyOtherClass();
$obj2->someMethod();
$obj2->someOtherMethod();
// Output:
// This is from someMethod.
// This is from someOtherMethod.In these examples, we define interfaces and use them in our PHP classes to ensure that our classes implement the required methods.
Interface constants
An interface can declare constants that every implementing class inherits. They are always public and cannot be overridden, which makes them useful for shared, fixed values:
<?php
interface HttpStatus {
const OK = 200;
const NOT_FOUND = 404;
}
class Response implements HttpStatus {
public function describe(): string {
return "Status " . self::OK;
}
}
echo (new Response())->describe() . PHP_EOL;
echo HttpStatus::NOT_FOUND . PHP_EOL;
// Output:
// Status 200
// 404For constants that can be overridden, use class constants inside a class instead.
Extending interfaces
Unlike classes, an interface can extend multiple other interfaces. The combined interface then requires all the methods from each parent:
<?php
interface Readable {
public function read(): string;
}
interface Writable {
public function write(string $data): void;
}
// A single interface that demands both capabilities
interface ReadWrite extends Readable, Writable {}
class File implements ReadWrite {
private string $buffer = "";
public function read(): string { return $this->buffer; }
public function write(string $data): void { $this->buffer = $data; }
}
$f = new File();
$f->write("hello");
echo $f->read() . PHP_EOL;
// Output: helloThis is how PHP simulates multiple inheritance — see PHP inheritance for single class inheritance with extends.
Type hints and instanceof
Because any implementing class satisfies the interface, you can type-hint the interface in function signatures and accept any matching object. Use instanceof to check at runtime:
<?php
interface Shape {
public function area(): float;
}
class Circle implements Shape {
public function __construct(private float $r) {}
public function area(): float { return 3.14159 * $this->r ** 2; }
}
function printArea(Shape $shape): void {
echo round($shape->area(), 2) . PHP_EOL;
}
$c = new Circle(2);
printArea($c);
var_dump($c instanceof Shape);
// Output:
// 12.57
// bool(true)Interface vs. abstract class
Both define a contract, but they solve different problems:
- An interface declares method signatures only and a class can implement many of them. Use it to describe a capability shared by unrelated classes.
- An abstract class can provide concrete methods and properties as well as abstract ones, but a class can extend only one. Use it to share implementation among related classes.
When you need to share code across classes without inheritance, reach for traits.
Benefits
Using the "interface" keyword has several benefits, including:
- Decoupling and flexibility: Interfaces allow you to swap implementations without modifying dependent code, making your system more adaptable.
- Easier testing: They enable mocking and stubbing, which simplifies unit testing and debugging.
- Simulating multiple inheritance: Since PHP does not support multiple class inheritance, a class can implement multiple interfaces to inherit behavior contracts from several sources.
Conclusion
The interface keyword lets you define a contract of required methods that any class can implement, regardless of its place in the class hierarchy. Combined with type hints and instanceof, interfaces make code more flexible, testable, and decoupled. Reach for an interface when you want to describe a capability; reach for an abstract class when you also need to share implementation.