What is an Access Modifier in PHP?

In PHP, access modifiers are used to control the visibility of class properties and methods.
Properties and methods can have access modifiers. It can be controlled where we can be accessed.
In other words, we can say that the Access Modifier allows us to alter the visibility of class members (properties and method).
There are three main access modifiers:
    1.  Public
    2.  Protected
    3.  Private

1. Public Access Modifier

   public access modifier is default, we can access the property or method from everywhere.

   In other words, we can say that it is open to use and access inside the class definition and outside the class definition.

   Public access modifiers can be accessed publicly, even from outside or inside of the scope of the class.

   Example 1

    <?php
  class Car {
      public $name;
      protected $color;
      private $Model;
  }
  $obj = new Car();
  echo $obj->name = 'THAR'; // OK
  echo $obj->color = 'RED'; // ERROR
  echo $obj->weight = 'LX Hard Top trim'; // ERROR
    ?>
    Output
   THAR
Example 2
<?php  
class Car{
public $name="RED THAR";  
  function display(){  
      echo $this->name."<br/>";  
  }
}
class Model extends Car{
function show(){  
    echo $this->name;  
   }  
}    
$obj= new Model;
echo $obj->name."<br>";  
$obj->display();
$obj->show();
?>
Output
RED THAR
RED THAR
RED THAR

 

2. Protected Access Modifier

   Protected access modifier is access  within the class and parent or inherited classes.
   In another words we can say the property or method can be accessed within the class and by classes derived from that class
   Example 1.
   <?php
  class Test{
      protected $a=200;
      protected $b=300;
          function add(){
              echo "Addition of (A+B): ".$sum=$this->a+$this->b."<br/>";
          }      
      }  
      class childtest extends test{
          function sub(){
              echo "Subtraction of (A-B): ".$sub=$this->a-$this->b."<br/>";
          }
      }  
      $obj= new childtest;
      $obj->add();
      $obj->sub();
    ?>  

3. Private Access Modifier

In Private access modifier the property or method can  be accessed within the class.
( it can't be access outside the class means in inherited class).

 

Example1
<?php
class Car{
  private $name="THAR CAR";
  private function show(){
      echo "This is private method of parent class";
  }
}
class Model extends Car{
 function show1(){
     echo $this->name;
 }
}
$obj= new Model();
$obj->show();
$obj->show1();
?>
Output Fatal error: Call to private method demo::show()

 

Example2
class parents{  
public $name="Thar";
protected $Model="Thar-02913";
private $price=50000;
public function show(){
  echo "Welcome : ".$this->name."<br/>";
  echo "Profile : ".$this->profile."<br/>";
  echo "Salary : ".$this->price."<br/>";
  }
}
class childs extends parents{
   public function show1(){
      echo "My New Car : ".$this->name."<br/>";
      echo "Model : ".$this->profile."<br/>";
      echo "Price : ".$this->salary."<br/>";
}
}
$obj= new childs;
$obj->show1();
?>

 

Example3
<?php
class parents{
public $name="Thar";
protected $model="Thar-02913";
private $price=50000;
public function show(){
  echo "Welcome : ".$this->name."<br/>";
  echo "Profile : ".$this->model."<br/>";
  echo "Salary : ".$this->price."<br/>";
  }
}
class childs extends parents{
 public function show1(){
      echo "My New Car : ".$this->name."<br/>";
      echo "Model : ".$this->model."<br/>";
      echo "Price : ".$this->salary."<br/>";
}
}
$obj= new childs;
$obj->show1();
?>
Output
My New Car : Thar
Model : Thar-02913
Price :
 


Prev Next