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 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Â
// BaseClassclass pub {Â Â Â Â public $tag_line = "A Computer Science Portal for Geeks!";Â Â Â Â function display() {Â Â Â Â Â Â Â Â echo $this->tag_line."<br/>";Â Â Â Â }}Â
// SubClassclass 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 Classclass pro {    protected $x = 500;    protected $y = 500;                 // Subtraction Function    function sub()     {        echo $sum=$this->x-$this->y . "<br/>";    }    } Â
// SubClass - Inherited Classclass 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 Classclass demo {Â Â Â Â private $name="A Computer Science Portal for Geeks!";Â Â Â Â Â Â Â Â Â private function show()Â Â Â Â {Â Â Â Â Â Â Â Â echo "This is private method of base class";Â Â Â Â }} Â
// Sub Classclass 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
<?phpclass 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 propertyecho $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 propertyecho $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.
Â



