PHP OOP - Access Modifiers
In PHP (and other object-oriented programming languages), access modifiers control the visibility of class properties and methods. PHP supports three main access modifiers:
1. Public
-
Accessible everywhere (inside the class, by child classes, and from outside the class).
-
This is the default visibility if no modifier is specified.
2. Protected
-
Accessible within the class itself and by classes derived from that class (subclasses), but not from outside the class.
3. Private
-
Accessible only within the class that defines it, not even by subclasses.
Summary Table
Modifier | Class Itself | Subclasses | Outside Class |
---|---|---|---|
public |
✅ | ✅ | ✅ |
protected |
✅ | ✅ | ❌ |
private |
✅ | ❌ | ❌ |