PHP Reference ยท Functions and Classes
PHP interfaces: interface and implements
Reference for PHP interfaces: declaring with interface, implementing with implements, constants, extending interfaces, instanceof checks and built-in interfaces.
An interface declares a set of public method signatures (and constants) that a class promises to provide. It contains no method bodies. Any class that implements the interface must define every method, and code can then depend on the interface instead of one concrete class.
A class can implement several interfaces, and an interface can extend several other interfaces. This is how PHP gets polymorphism without multiple inheritance of classes.
interface Logger { public function log(string $message): void; } class EchoLogger implements Logger { public function log(string $message): void { echo $message, "\n"; } }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
interface Name [extends OtherInterface, ...] { public const NAME = value; public function method(Type $arg): ReturnType; public static function create(): static; } class Impl [extends Base] implements Name, Another { // must implement all methods with compatible signatures }
Rules:
- All methods are public and bodiless. Visibility modifiers other than
publicare errors. - Interfaces cannot have instance properties. PHP 8.4 allows property declarations that are public and hook-based (for example
public string $name { get; }). - Interface constants can be overridden by implementing classes unless declared
final(PHP 8.1+). - Method signatures in the class must be compatible: parameter types may widen, return types may narrow.
Errors: a class that misses a method is a fatal error at compile time. new on an interface throws Error.
Usage notes
- Type against the interface in function parameters and return types, not the concrete class. That makes code easy to test and extend.
instanceofand type declarations accept any implementing class.- Marker interfaces with no methods tag classes (the standard library uses
Countable,ArrayAccess,StringableandJsonSerializablefor real behavior). Stringableis added automatically to any class that defines__toString().- Interface default methods do not exist. Share code with an abstract class or a trait.
- Enums can implement interfaces.
- Naming: use meaningful names (
Logger,Cache); aInterfacesuffix is a matter of team style. - Built-in interfaces to know:
Traversable,Iterator,IteratorAggregate,Countable,ArrayAccess,JsonSerializable,Stringable,Throwable,UnitEnum,BackedEnum. - Inspection:
class_implements(),interface_exists(),is_a()andReflectionClass::getInterfaceNames().