🐘 PHP
Beginner
What is an interface in PHP?
Answer
An interface in PHP defines a contract — a set of public method signatures that any implementing class must provide. Interfaces have no implementation code. Declare with interface and implement with implements: class Car implements Drivable, Serializable { }. A class can implement multiple interfaces (unlike single class inheritance). All methods in an interface are public and abstract by default. Since PHP 8.0, interfaces can include constants. Since PHP 8.1, interfaces can include enum cases as constants. Interfaces are ideal for defining shared contracts between unrelated classes, enabling polymorphism without forcing an inheritance relationship. Type-hint against interfaces for flexibility: function drive(Drivable $vehicle).