PHP OOP Access Modifiers

PHP OOP access modifiers are used to determine the visibility of class members inside or outside of the class & basically, it is used by class members such as properties and methods.

PHP object-oriented programming provides three types of access modifiers. These are:

  1. Public
  2. Private
  3. Protected

Public Access modifier

Public access modifiers can be accessed within the class as well outside of the class.

Source Code

          <?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();
//explain if you like 
?>
        
Try it now

Protected Access Modifier

A protected access modifier can be accessed inside the class as well as it will be accessed by subclasses too.

Source Code

          <?php
class Demo{
protected $x=500;
protected $y=500;
function add(){
 echo $sum=$this->x+$this->y."<br/>";
 }
}	
class Child extends Demo{
function sub(){
 echo $sub=$this->x-$this->y."<br/>";
 }
}	
$obj= new Child;
$obj->add();
$obj->sub();

?>
        
Try it now

Private Access Modifier

Private access modifier can be access only within the class. It can not be access outside of the class.

Source Code

          <?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();
?>
        
Try it now

How To Access A Private Property?

Private access modifier can be accessed through the following methods.

  • Setters that set the values of the private properties.
  • Getters that get the values of the private properties.

Source Code

          <?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 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();
 
?>
        
Try it now

Why do we need access modifiers?

Access modifier is used to limit the class members accessing from outside of the class. After defining a property or method as private, only methods that are within the class are allowed to access it. So, in order to interact with private methods and properties, we need to provide public methods.

Source Code

          <?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 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();
 
?>
        
Try it now

Web Tutorials

PHP OOP Access Modifiers
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4