PHP OOPLesson 47Free preview
PHP Access Modifiers
This lesson is part of PHP · All levels
Summary
Access modifier determines the extent to which the property and method can be accessed, namely public, protected, and private. Public can be accessed from anywhere, protected only from the class itself and its descendants, while private only from within the same class. Hiding data with a modifier is a basic step in maintaining the integrity of the object.
Key points
- Public: open to all contexts, protected: for class and child, private: only class itself.
- If visibility isn't written, PHP considers it public.
- Accessing private members from outside the class would result in an error in PHP 8.
- Private property should be accessed through public methods such as getters and setter.
Full lesson
Code examples and quizzes
Full lessons, code examples, and quizzes are available to subscribers. 5 questions.