PHP OOP Abstract Classes
PHP OOP – Abstract Classes
An abstract class is a class that cannot be instantiated (you cannot create objects from it).
It is used as a base/blueprint for other classes.
🔥 Key Points About Abstract Classes
✔ You cannot create an object of an abstract class
✔ Abstract class may have abstract methods (no body)
✔ Child class must override abstract methods
✔ Can have normal methods also
✔ Supports properties & constructors
✔ Supports protected & private members
✔ Supports inheritance
🟦 1️⃣ Basic Abstract Class Example
You cannot do:
🟩 2️⃣ Child Class Must Define Abstract Method
🟧 3️⃣ Abstract Class with Normal Method
🟥 4️⃣ Abstract Class with Constructor
🟫 5️⃣ Multiple Abstract Methods
🟪 6️⃣ Why Use Abstract Classes?
✔ Forces child classes to follow rules
✔ Ensures structure/pattern
✔ Useful when many classes share common behavior
✔ Helpful in large applications
✔ Used widely in frameworks like Laravel
Example: Laravel controller
All controllers extend it.
🟧 7️⃣ Abstract Class vs Interface (Important)
| Feature | Abstract Class | Interface |
|---|---|---|
| Methods | Both normal + abstract | Only abstract (PHP 7), can have default in PHP 8 |
| Constructor | ✔ Yes | ❌ No |
| Properties | ✔ Yes | ✔ (public only) |
| Multiple inheritance | ❌ No | ✔ Implement multiple interfaces |
| Use case | When base class is needed | When only rules are needed |
🟩 Practical Example
Making a universal payment system:
🎯 Summary (Easy to Remember)
| Concept | Meaning |
|---|---|
| abstract class | Cannot make object, used as base |
| abstract method | Must be implemented by child |
| extends | Child inherits abstract class |
| Purpose | Enforce structure in child classes |

