The visibility of variables, particularly with properties and methods in object-oriented programming, is a key concept that is of great importance to every developer. In PHP, the visibility modifiers public, private, and protected determine who can access specific properties and methods. These concepts help to implement data encapsulation, making object-oriented programs more robust and maintainable. In this guide, you will learn how these modifiers are used and what effects they can have on the structure of your programming.
Key insights
- public: properties are accessible from anywhere.
- private: properties are accessible only within the class itself.
- protected: properties are accessible within the class and in derived classes.
Step-by-step guide
1. Creating a simple class with a public property
To start, we will create a class called Car, in which we will define a public property named speed. This will give you a clear picture of how to access this property from outside.

You can now create an object of this class:

Now, if you set the speed of Car to 30, 50, or even 130, you have no access restrictions – that is the strength of public visibility.
2. Introducing maximum speed with a private property
Now we will add a private property maxSpeed and modify the method to set only allowed speeds. This way, you can ensure that only valid values are assigned.
With this method, you can set the speed while ensuring that no error occurs due to too high values.

3. Using getter and setter methods for private properties
Since maxSpeed is private, you can only make it accessible through methods within the class. The following example shows how to implement getter and setter methods.

Through these methods, you achieve encapsulation and control over the properties from outside.

4. Understanding the protected modifier
Now let's look at the protected modifier. This gives classes that inherit from other classes access to certain properties. This means that subclasses can continue to use or override a specific method or property.

In this case, race cars can access the protected property, while external classes cannot. This allows targeted behavior for derived classes to be coded.
5. Deciding on visibility
An important decision you need to make concerns the visibility of your properties. Generally, you should use private, unless it is obvious that a property is needed within inheritance. In that case, choose protected.

Keep in mind that if you are in a maintenance phase or working on a new feature, you can still change your access controls later if necessary.
Summary – Visibility of properties: public, private, and protected in PHP
The visibility of properties in PHP is crucial for understanding data encapsulation and object-oriented programming. With the modifiers public, private, and protected, you can achieve precision and control over access to properties and methods in your classes. By applying these principles correctly, you greatly enhance the maintainability and security of your code.
Frequently asked questions
What does public mean in PHP?public means that the property or method is accessible from anywhere.
What does private mean in PHP?private means that the property or method can only be called from the own class itself.
What does protected mean in PHP?protected means that the property or method can be called from the own class and from derived classes.
When should I use private?You should use private by default to maximize visibility and control.
When can I switch to protected?Switch to protected when a property is needed within derived classes and you want to prevent access from outside.