PHP Basic Tutorial
MySQL Connection
PHP Advanced
Access modifiers in PHP control the visibility of class properties and methods.
PHP provides three types of access modifiers:
public
- Accessible from anywhere (inside or outside the class). This is defaultprotected
- Accessible within the class and inherited (child) classes onlyprivate
- Accessible only inside the class that defines itIn the following example we have added three different access modifiers to three properties (brand, engine, and price). Here, if you try to access the brand property it will work fine (because the brand property is public, and can be accessed from everywhere). However, if you try to access the engine or color property it will result in a fatal error (because the engine and color property are protected and private):
Try it yourself