Multiple Interfaces
A class can implement multiple interfaces, separated by commas. This is how PHP achieves multiple-inheritance behavior.
A class can implement multiple interfaces, separated by commas. This is how PHP achieves multiple-inheritance behavior.
<?php
interface Printable {
public function print(): void;
}
interface Exportable {
public function toArray(): array;
public function toJson(): string;
}
interface Cacheable {
public function getCacheKey(): string;
}
class Invoice implements Printable, Exportable, Cacheable {
public function print(): void { echo "Invoice #1"; }
public function toArray(): array { return ["id" => 1]; }
public function toJson(): string { return json_encode($this->toArray()); }
public function getCacheKey(): string { return "invoice:1"; }
}
A class can implement as many interfaces as needed — combine multiple small interfaces rather than one large one (Interface Segregation Principle).