Open In App

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

Improve
Improve
Like Article
Like
Save
Share
Report

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 child 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: 
 

php




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


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: 
 

php




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


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: 
 

php




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


Output: 
 

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

Miscellaneous example: 
Example: 
 

php




<?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 private property
$obj2->Display(); //Displays all properties
?>


Output: 
 

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

 

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.

 



Last Updated : 06 Oct, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads