The use of access modifiers is in encapsulation. It changes the visibility of class members and determines which parts of the program have access to them. This tutorial describes the use of PHP access modifiers with examples.



What Are PHP Access Modifiers?

In object-oriented programming, access modifiers define the visibility of class members. Like other OOP programming languages, PHP has three access modifiers, public, private, and protected. They are described below with examples:

Access Modifiers Description
public Any property or function defined as a public access scope can be accessed inside the class and by any child class. In other words, they will be available for access from anywhere.
private Any property or function defined as private access scope can only be accessed within the class where it is declared.
protected Protected members are not accessible outside the class but in which it is defined and are accessible by any child class.

PHP Public Access Modifier

Example:

<?php
/* Parent Class */
class ParentClass{

  /*Class Members*/
  public $A = 130;
  public $B = 70;

  public function addition(){
    echo "The sum is ", $this->A + $this->B;
  }
}

/* Child class */
class ChildClass extends ParentClass{
  public function subtract(){
    echo ", and the difference is ", $this->A - $this->B,"." ;
  }
}

$Obj = new ChildClass;
$Obj->addition() ;
$Obj->subtract() ;
?>

Output:

The sum is 200, and the difference is 60.

Another Example of PHP Public Access Modifier

Example:

<?php
/* Parent Class */
class ParentClass{
  public $Name = "W3schools.in";
  function Display()  {
    echo $this->Name;
  }
}

/* Child class */
class ChildClass extends ParentClass{
  function ShowDetails()  {
    echo $this->Name;
  }
}

$Obj = new ChildClass;
echo $Obj->Name;
$Obj->Display();
$Obj->ShowDetails();
?>

Note: If the access modifier is not specified, all classes and their members are assumed to be public by default.


PHP Private Access Modifier

Example:

<?php
/* Parent Class */
class ParentClass
{
  private function Display(){
    echo "This is a private class method, and we can’t extend it.";
  }
}

/* Child class */
class ChildClass extends ParentClass{}

$Obj = new ChildClass;
$Obj->Display();
?>

Output:

( ! ) Fatal error: Uncaught Error: Call to private method ParentClass::Display() from context '' in D:\wamp64\www\test1.php on line 14

Another Example of PHP Private Access Modifier

Example:

<?php
/* Parent Class */
class ParentClass{
  /* Private members */
  private $x = 55 ; 
  private $y = 5 ;

  /*private member function*/
  private function div(){
    echo $this->x / $this->y ;
  }
}

/* Child class */
class ChildClass extends ParentClass{
  function mul(){
    echo $this->x * $this->y ;
  }
}

$Obj = new ChildClass;
$Obj->div();
$Obj->mul();
?>

The above code will not return the result because the data members and functions are private, so the child class cannot access them, resulting in a fatal error.

Output:

( ! ) Fatal error: Uncaught Error: Call to private method ParentClass::div() from context '' in D:\wamp64\www\test1.php on line 28

PHP Protected Access Modifier

Example:

<?php
/* Parent Class */
class Add{

  /*Class Members*/
  protected $A = 50;
  protected $B = 10;

  protected function addition(){
    echo "The sum is ", $this->A + $this->B;
  }
}

/* Child class */
class Sub extends add{
  function subtract(){
    $this->addition();
    echo ", and the difference is ", $this->A - $this->B,"." ;
  }
}

$Obj = new Sub;
/* $this->addition(); */ /*it will display fatal error.*/
$Obj->subtract() ;
?>

Output:

The sum is 60, and the difference is 40.


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram