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:
<?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 // 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 // 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 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 ?> |
Output:
It will display error because private class data can not be accessed outside the class
Recommended Posts:
- Know your public and private IP addresses
- Replacing 'public' with 'private' in "main" in Java
- Private Methods in Java 9 Interfaces
- Understanding "static" in "public static void main" in Java
- Web 1.0, Web 2.0 and Web 3.0 with their difference
- What is the difference between GUI and CUI?
- What is the difference between the | and || or operator in php?
- Difference between SSH and Telnet
- Difference between Where and Group By
- Difference Between HTML and ASP
- Difference between JavaScript and Php
- What is the Difference between Website and Web Portal
- How to calculate the difference between two dates in PHP?
- Difference between std::quick_exit and std::abort
- Difference between bindParam and bindValue in PHP
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.



