What Is The Difference Between Public, Private, And Protected Example

28-Aug-2021

.

Admin

What Is The Difference Between Public, Private, And Protected Example

Hi guys,

Today i will explained What Is The Difference Between Public, Private, And Protected. This example is so easy to use in php.

In this article, we will describe what these keywords are used for and which one to use in defining any class method or properties.

These keywords are called visibility modifier. The visibility of property, constant or method can be defined prefixing keywords public, protected or private. If no visibility is defined to property, the property will be defined as public. Let's take examples in PHP language.

So let's start to the example.

Public


When you declare a method, constant or property as public, those methods, constants and properties can be accessed:

In the same class in which it is declared.

The classes which extends the same class.

Instances of the class.

Example :

<?php

class Vehicle {

public $year = '2020';

public function start() {

return 'ignition';

}

}

class Car extends Vehicle {

// we can access and redeclare public property and methods

public $year = '2021';

}

$honda = new Car();

echo($honda->year);

echo($honda->start());

Output

-2021

-ignition

You can declare method, constant or property as public when you want to access those at anywhere.

Protected

When you declare any method or property to private, those can be accessed:

In the same class.

The classes that inherit the above declared class

Example :

<?php

class Vehicle {

protected $year = '2020';

protected $color = 'red';

}

class Car extends Vehicle {

protected $color = 'blue'; // we can redeclare protected

public function getYear() {

return $this->year; // we can access variable here

}

public function getColor() {

return $this->color;

}

}

$honda = new Car();

echo($honda->getYear());

echo($honda->getColor());

echo($honda->year);

Output

-2020

-blue

-Uncaught Error: Cannot access protected property Car::$year

So when you want to prevent any property or method to be access from the class object, then you may define them as protected.

Private

When you declare any method or property as private, those can be accessed only in the same class. It can not even accessible in inherit class.

Example :

<?php

class Vehicle {

private $year = '2020';

public function getYear() {

return $this->year; // we can access private variable here

}

public function setYear($year) {

$this->year = $year;

}

}

$honda = new Vehicle();

echo($honda->getYear());

echo($honda->setYear('2021'))

echo($honda->getYear());

echo($honda->year);

Output

-2020

-we can set private property using setter function

-2021

-Uncaught Error: Cannot access private property Vehicle::$year

So if you want to property or method to be declare private if can be used into the same class only. Generally helper function declare as private as it shouldn't be used outside class.

Now you can check your own. I hope it can help you...

#PHP 8

#PHP