PHP OOP Abstract Classes

PHP OOP Abstract Classes
An abstract class is a class that cannot be instantiate (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
Basic Abstract Class Example
You cannot do:
Child Class Must Define Abstract Method
Abstract Class with Normal Method
Abstract Class with Constructor
Multiple Abstract Methods
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.
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 |

