LearnAccess modifiers: public vs. private

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 explanation. The public access modifier is only one of several modifiers that we use. In this tutorial, 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.

<?php
 
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 we access a 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.

<?php
 
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 benz";
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.

<?php
 
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();
//Sets the car’s model
$mercedes -> setModel("Mercedes benz");
//Gets the car’s model
echo $mercedes -> getModel();
 
?>

Result:
The car model is Mercedes benz

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 private $model property, by defining the allowed alternatives for models in the public setModel() method. For this purpose, we define inside the setModel() method an array of allowed car models, and check that only these models are assigned to the $model property.

<?php
 
class Car {
 
  //the private access modifier denies access to the property 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
    $allowedModels = array("Mercedes benz","BMW");
 
    if(in_array($model,$allowedModels))
    {
      $this -> model = $model;
    }
    else
    {
      $this -> model = "not in our list of models.";
    }
  }
  
  public function getModel()
  {
    return "The car model is  " . $this -> model;
  }
}
 
 
$mercedes = new Car();
//Sets the car’s model
$mercedes -> setModel("Mercedes benz");
//Gets the car’s model
echo $mercedes -> getModel();
 
?>

Conclusion

So far, we have learned about two access modifiers: public, which allows outside functions to modify the code inside a class, and private, that prevents any code from outside the class to change the properties and methods which it protects. We saw that, in order to modify private methods and properties, we can use public methods that have the privilege to interact with the code outside the scope of the class. Click here to practice the subject.

In the next tutorial, you will learn about magic methods and constants. These are kinds of candies that PHP provides, but they should be used with much caution.

comments powered by Disqus