PHP
Multiple interfaces
Learn to combine contracts by implementing multiple interfaces on a class.
Implementing multiple interfaces
A class can implement multiple interfaces at once. Interface names are separated by commas: class FileLogger implements Logger, Configurable { ... }.
The class must implement all methods from both interfaces. Logger's log() and Configurable's configure() must be defined in the class.
This enables contract composition: a class can be both loggable and configurable. While only one parent class can be extended, unlimited interfaces can be implemented.
An interface composition example
Let Logger and Configurable be two interfaces. The FileLogger class implements both: class FileLogger implements Logger, Configurable { ... }.
Inside the class both public function log(string $message): void and public function configure(array $options): void are defined. The methods of two contracts merge in one class.
So FileLogger can be used where either a Logger or a Configurable is expected. The class carries two capabilities at once.
Exercise Document class implements two interfaces
First define interface Renderable { public function render(): string; } and interface Cacheable { public function cacheKey(): string; }. Then write class Document implements Renderable, Cacheable with a private string $title property and a __construct(string $title) that runs $this->title = $title. The render() method does return "[doc] " . $this->title, the cacheKey() method does return "doc_" . $this->title. Then create $doc = new Document("Report") and print echo $doc->render() . "\n" and echo $doc->cacheKey() . "\n".
Show example solution
First define both interfaces. Then inside class Document implements Renderable, Cacheable write private $title, __construct, render and cacheKey.
Example solution:
<?php
// Define the Document class
interface Renderable {
public function render(): string;
}
interface Cacheable {
public function cacheKey(): string;
}
class Document implements Renderable, Cacheable {
private string $title;
public function __construct(string $title) {
$this->title = $title;
}
public function render(): string {
return "[doc] " . $this->title;
}
public function cacheKey(): string {
return "doc_" . $this->title;
}
}
$doc = new Document("Report");
echo $doc->render() . "\n";
echo $doc->cacheKey() . "\n";
Test yourself
-
What must a class implementing two interfaces do?
- Implement only one's methods
- Only declare constants
- extends both with inheritance
- Implement all methods of both
Show answer
Implement all methods of both — Correct. A class implementing two interfaces writes all methods of both.
-
Write multiple implements
class FileLogger ____ Logger, Configurable {
public function log(string $message): void {}
}
Show answer
class FileLogger implements Logger, Configurable {
public function log(string $message): void {}
}
-
Match the multiple interface expressions
- implements A, B
- Two contracts at once
- comma
- Interface separator
- all methods
- Written in the class
- missing method
- Triggers a fatal error
-
How are interface names separated in multiple implementation?
- with a comma
- with a dot
- with a plus sign
- with the word and
Show answer
with a comma — Correct. Multiple interface names are separated by commas: implements A, B.
Next lesson: Polymorphism