What is the difference between public, private, and protected in PHP?

Public, private and protected are called access modifiers. Just like C++, PHP also have three access modifiers such as public, private and protected. The visibility of a property, a method or a constant can be defined by prefixing the declaration with these keywords.

  • If the class member declared as public then it can be accessed everywhere.
  • If the class members declared as protected then it can be accessed only within the class itself and by inheriting and parent classes.
  • If the class members declared as private then it may only be accessed by the class that defines the member.

Public Access modifier: This modifier is open to use inside as well as outside the class.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// BaseClass
class pub {
    public $tag_line = "A Computer Science Portal for Geeks!";
    function display() {
        echo $this->tag_line."<br/>";
    }
}
  
// SubClass
class child extends pub {
    function show(){
        echo $this->tag_line;
    }
  
// Object Declaration
$obj= new child;
  
// A Computer Science Portal for Geeks!
echo $obj->tag_line."<br/>"
  
// A Computer Science Portal for Geeks!
$obj->display(); 
  
// A Computer Science Portal for Geeks!
$obj->show(); 
?>

chevron_right


Output:



A Computer Science Portal for Geeks!
A Computer Science Portal for Geeks!
A Computer Science Portal for Geeks!

Protected Access modifier: This modifier is open to use within the class in which it is defined and its parent or inherited classes.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Base Class
class pro {
    protected $x = 500;
    protected $y = 500;
              
    // Subtraction Function
    function sub() 
    {
        echo $sum=$this->x-$this->y . "<br/>";
    }     
  
// SubClass - Inherited Class
class child extends pro {
    function mul() //Multiply Function
    {
        echo $sub=$this->x*$this->y;
    }
  
$obj= new child;
$obj->sub();
$obj->mul();
?>

chevron_right


Output:

0
250000

Private Access modifier: This modifier is open to use within the class that defines it. ( it can’t be accessed outside the class means in inherited class).

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Base Class
class demo {
    private $name="A Computer Science Portal for Geeks!";
      
    private function show()
    {
        echo "This is private method of base class";
    }
  
// Sub Class
class child extends demo {
    function display()
    {
        echo $this->name;
    }
  
// Object Declaration
$obj= new child;
  
// Uncaught Error: Call to private method demo::show()
$obj->show(); 
  
//Undefined property: child::$name
$obj->display(); 
?>

chevron_right


Output:

It will display error because private class data can not be accessed outside the class

Miscellaneous example:
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
class BaseClass
{
    public $public = 'Public';
    protected $protected = 'Protected';
    private $private = 'Private';
  
    function Display()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
  
$obj = new BaseClass();
echo $obj->public
echo $obj->protected; // Cannot access protected property
echo $obj->private; // Cannot access private property
$obj->Display();  //Displays all properties
  
class SubClass extends BaseClass
{
    public $public = 'Public Sub Class';
    protected $protected = 'Protected Sub Class';
  
    function Display()
    {
        echo $this->public;
        echo $this->protected;
        echo $this->private;
    }
}
  
$obj2 = new SubClass();
echo $obj2->public
echo $obj2->protected; // Cannot access protected property
echo $obj2->private// Cannot access priavte property
$obj2->Display(); //Displays all properties
?>

chevron_right


Output:

It will display error because private class data can not be accessed outside the class


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.