What is the difference between public, private, and protected in PHP?
Last Updated :
06 Oct, 2021
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
class pub {
public $tag_line = "A Computer Science Portal for Geeks!" ;
function display() {
echo $this ->tag_line. "<br/>" ;
}
}
class child extends pub {
function show(){
echo $this ->tag_line;
}
}
$obj = new child;
echo $obj ->tag_line. "<br/>" ;
$obj ->display();
$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
class pro {
protected $x = 500;
protected $y = 500;
function sub()
{
echo $sum = $this ->x- $this ->y . "<br/>" ;
}
}
class child extends pro {
function mul()
{
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
class demo {
private $name = "A Computer Science Portal for Geeks!" ;
private function show()
{
echo "This is private method of base class" ;
}
}
class child extends demo {
function display()
{
echo $this ->name;
}
}
$obj = new child;
$obj ->show();
$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 ;
echo $obj -> private ;
$obj ->Display();
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 ;
echo $obj2 -> private ;
$obj2 ->Display();
?>
|
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.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...