Access modifiers: public vs. private
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
class Car { // Public methods and properties public $model; public function getModel() { return "The car model is " . $this -> model; } } $mercedes = new Car(); // Here we access a property from outside the class $mercedes -> model = "Mercedes"; // Here again we access another method from outside the class echo $mercedes -> getModel(); |
Result:
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.
1 2 3 4 5 6 7 8 9 10 11 12 |
class Car { // Private private $model; public function getModel() { return "The car model is " . $this -> model; } } $mercedes = new Car(); // We try to access a private property from outside the class. $mercedes -> model = "Mercedes"; echo $mercedes -> getModel(); |
Result:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
class Car { // The private access modifier denies access to the method // from outside the class’s scope private $model; // The public access modifier allows the access to the method // from outside the class public function setModel($model) { $this -> model = $model; } public function getModel() { return "The car model is " . $this -> model; } } $mercedes = new Car(); // Set the car’s model $mercedes -> setModel("Mercedes"); // Get the car’s model echo $mercedes -> getModel(); |
Result:
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
class Car { // The private access modifier denies access to the // method from outside the class’s scope private $model; // The public access modifier allows the access to // the method from outside the class public function setModel($model) { // Validate that only certain car models are assigned // to the $carModel property if(in_array($model,$allowedModels)) { $this -> model = $model; } } public function getModel() { return "The car model is " . $this -> model; } } $mercedes = new Car(); // Set the car’s model $mercedes -> setModel("Mercedes"); // Get the car’s model echo $mercedes -> getModel(); |