A PHP interface declares methods that a class promises to provide, without any code: interface Shape { public function area(): float; }. A class that writes implements Shape must define area(), and any function that type-hints Shape will accept that class.
describe() does not know or care which class it receives. It only relies on the promise the interface makes, so a new Triangle implements Shape works with it without changing a line.
Program to an interface
The usual reason to write an interface is to let code depend on a capability rather than a specific class. Here an order service accepts any Notifier, so the same code can send email in production and collect messages in a test:
Implement multiple interfaces
A class extends at most one parent, but it can implement as many interfaces as it needs. List them after implements, separated by commas, and define the methods of all of them:
JsonSerializable is one of PHP's built-in interfaces: implementing it controls what json_encode() produces for the object.
Interface constants
An interface can declare constants. Every implementing class inherits them, and you can read them through the interface name or the class name:
Interfaces cannot have properties in PHP 8.3, only methods and constants. The methods may be static: public static function fromArray(array $data): static; in the interface obliges each class to define that static method. For a fixed set of related values, an enum is often a better fit than a list of interface constants.
An interface can extend other interfaces
Interfaces use extends too, and unlike classes they can extend several at once. A class implementing the child interface must define the methods of the whole chain:
Built-in interfaces: Countable, IteratorAggregate, Stringable
PHP ships interfaces that hook objects into the language. Countable makes count($obj) work, IteratorAggregate makes foreach work, and Stringable is implemented automatically by any class with a __toString() method:
Forgetting a method
If a class claims to implement an interface but misses a method, PHP stops with a fatal error that names the missing method:
Add a total() method and the script runs. An abstract class may leave interface methods undefined, which is the escape hatch the error message suggests.
Interface vs abstract class
Both define methods a class must have, and both can be used as a type hint. The difference is what else they may contain and how many a class can use:
| Interface | Abstract class | |
|---|---|---|
| Method bodies | none (only signatures) | abstract and finished methods |
| Properties | no | yes |
| Constructor | can declare one, rarely useful | yes, with code |
| Visibility of methods | public only | public, protected, private |
| Constants | yes | yes |
| How many per class | any number (implements A, B, C) | one (extends Base) |
| Typical use | a capability: Exportable, Countable | shared code for a family: Model, Controller |
They combine well: define the interface for the type hint, and offer an abstract base class that implements it with the common code. Classes that need a different base can still implement the interface directly.
Frequently Asked Questions
What is an interface in PHP?
A list of public method signatures with no bodies: interface Logger { public function log(string $msg): void; }. A class that says implements Logger must define every method in it, and any code that type-hints Logger accepts that class.
Can a PHP class implement multiple interfaces?
Yes. List them after implements, separated by commas: class File implements Readable, Writable, Countable. The class must define the methods of all of them. This is how PHP covers what multiple inheritance does in other languages.
What is the difference between an interface and an abstract class in PHP?
An interface only declares public methods (and constants); it has no properties and no method code, and a class can implement many. An abstract class can hold properties, constructors and finished methods next to its abstract ones, but a class can extend only one. Use an interface for a capability ("can be exported"), an abstract class for shared code in a family of classes.
Can a PHP interface have properties?
Not in PHP 8.3: an interface may contain method signatures and constants (const VERSION = 2;), but no properties. Property declarations in interfaces (property hooks) only arrived in PHP 8.4.
How do I check if an object implements an interface in PHP?
Use instanceof: $obj instanceof Countable is true when the object's class implements the interface, directly or through a parent class. class_implements($obj) returns the names of all interfaces the class implements.