PHP OOP Interfaces

PHP OOP Interfaces
In PHP OOP Interfaces is like a pure blueprint.
It only contains method declarations, not method bodies.
A class that implements an interface must define all its methods.
What Interfaces Do?
- Force a class to follow a structure
- Define rules
- Allow multiple inheritance (a class can implement MANY interfaces)
- Improve code architecture and flexibility
Basic Interface Example
Interface with Multiple Methods
A Class Can Implement Multiple Interfaces
PHP does not support multiple class inheritance,
but supports multiple interface inheritance.
Interface Methods Are Always Public
If you write:
In the implementing class, this is wrong:
Correct:
Interface Constants
Interfaces can contain constants:
Use it like:
Interface Extending Another Interface
Practical Example – Payment Gateway
Interface vs Abstract Class (Easy Table)
| Feature | Interface | Abstract Class |
|---|---|---|
| Methods | Only abstract (PHP 7), allowed body in PHP 8 | Abstract + Normal |
| Variables | Only constants | Properties allowed |
| Constructor | No | Yes |
| Multiple inheritance | Yes | No |
| Use case | Define rules | Base class with partial implementation |
Summary (Easy to Remember)
| Concept | Meaning |
|---|---|
| Interface | Blueprint with method declarations |
| implements | A class follows the rules of interface |
| Must override | All methods must be defined |
| Multiple inheritance | Class can implement many interfaces |

