Search⌘ K
AI Features

Why Do We Need Access Modifiers?

Explore the purpose of access modifiers in PHP object-oriented programming to control access to class properties and methods. Learn how public and private modifiers protect data and how public methods can safely interact with private properties, ensuring data validation and secure programming practices.

We need access modifiers to limit the modifications that code from outside the classes can make to the class’s methods and properties.

Once we define a property or method as private, only methods 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.

Example: Validating checks using access modifiers

In our example, we can validate that only specific car models can make their way and be assigned to the $model property by defining the allowed alternatives for models in the setModel() method.

PHP
<?php
class Car {
// public access modifier
public $color;
// 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 = ["Mercedes", "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();
// Set the car’s model and color
$mercedes -> setModel("Mercedes");
$mercedes -> color = "Red";
// Get the car’s model
echo $mercedes -> getModel() . " and its color is " . $mercedes -> color;
?>

In line 2, we write a Car class with $color as a public property and $model as a private property.

In line 13, we write a public setModel() method to interact with the private $model property. We define an array of allowed car models inside the setModel() method.

In line 18, we check that only allowed models are assigned to the $model property.

In line 28, we write a public getModel() method to return the value of $model.

In lines 33 to 37, we create a $mercedes object of the Car class and set its $model to Mercedes and color to Red.

In line 40, we print the $model and color property of the $mercedes object.

In line 36, we can set different models for the $mercedes object using the setModel() method and observe the results

In this chapter, we learned about two access modifiers:

  • public access modifiers, which allow outside functions to modify the code inside a class.
  • private access modifiers, which prevent any code from outside the class to change the properties and methods.

We also saw that, in order to modify private methods and properties, we can use public methods that have the privileges to interact with code outside the scope of the class.