Access modifiers: public vs. private

606 0

In the previous tutorials, we used the public access modifier in front of the methods and properties in our classes without any explanations. The public access modifier is only one of several modifiers that we use. In this tutorials, we will learn about another modifier called the private access modifier. While the public access modifier allows a code from outside or inside the class to access the class’s methods and properties, the private modifier prevents access to a class’s methods or properties from any code that is outside the class.

The public access modifier
The following example should already be familiar to you. In the example, the class’s property and method are defined as public, so the code outside the class can directly interact with them.

The car model is Mercedes

The private access modifier
We can prevent access to the properties and methods inside our classes if we define them with the private access modifier instead of the public access modifier.
In the following example, we define the property $model as private, and when we try to set its value from outside the class, we encounter a fatal error.

Fatal error: Cannot access private property Car::$model

How to access a private property?
We saw that we have no access to private properties from outside the class, but we still have to somehow set and get the properties’ values. In order to interact with private properties, we use public methods because they can interact with both the code outside of the class’s scope as well as the code inside the class. The public methods that can interact in this manner are commonly divided into two kinds of methods:
• Setters that set the values of the private properties.
• Getters that get the values of the private properties.

In the following example, we will see that we can get and set the value of a private property, $carModel, through the use of setter and getter methods. We will use the setModel() method in order to set the value of the car model, and the getModel() method to get the value of the property.

The car model is Mercedes

Why do we need access modifiers?
We need access modifiers in order to limit the modifications that code from outside the classes can do to the classes’ methods and properties. Once we define a property or method as private, only methods that are within the class are allowed to approach it. So, in order to interact with private methods and properties, we need to provide public methods. Inside these methods, we can put logic that can validate and restrict data that comes from outside the class.

In our example, we can validate that only certain car models can make their way, and be assigned to the $model property, by defining the allowed alternatives for models in the getModel() method. For this purpose, we define inside the getModel() method an array of allowed car models, and check that only these models are assigned to the $model property.



In this article

Join the Conversation