PHP OOP Interfaces

🔌 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


 1️⃣ Basic Interface Example


 


 2️⃣ Interface with Multiple Methods


 


 3️⃣ A Class Can Implement Multiple Interfaces


 

PHP does not support multiple class inheritance,
but supports multiple interface inheritance.


 4️⃣ Interface Methods Are Always Public

If you write:


In the implementing class, this is wrong:


Correct:



 5️⃣ Interface Constants

Interfaces can contain constants:


Use it like:



 6️⃣ Interface Extending Another Interface


 


 7️⃣ 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

You may also like...