🐘 PHP Beginner

What is an interface in PHP?

Why Interviewers Ask This

Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for PHP development. It reveals whether you understand the building blocks that more complex concepts rely on.

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).

Common Mistake

Candidates often give textbook answers here. Interviewers are more impressed when you relate the concept to a specific problem you solved in a real PHP project.