What is Visibility or Access modifier in PHP?
Accessibility of any property, constant or method is determined by visibility. Visibility is usually declared before constant, property and method.
How many visibility or access modifier in PHP?
There are three types of visibility in PHP:
- Public
- Protected
- Private
Now let’s try to understand each Visibility with example:
Public:
If we want to use any property, method and constant of any class within the class, in child class and outside of the class, then these constant, property and method have to be declared as public. Let’s get a better understanding by this example:
<?php class GrandFather { public $name='Masud Alam'; // A public variable public function getName(){ return $this->name; } } class Daddy extends GrandFather // Inherited class { function displayGrandFaName() { return $this->name; // The public variable will be available to the inherited class } } $grandFa=new GrandFather; echo $grandFa->getName(),"\n"; // Inherited class Daddy wants to know Grandpas Name $daddy = new Daddy; echo $daddy->displayGrandFaName(),"\n"; // Prints 'Masud Alam' // Public variables can also be accessed outside of the class! $outsiderWantstoKnowGrandpasName = new GrandFather; echo $outsiderWantstoKnowGrandpasName->name; // Prints 'Masud Alam ?>
Explanation: Observe, as our GrandFather class’s $name property is public, we were able to access the $name property from the current class, child class and even outside of the class.
Protected:
If we want to use any property, method and constant of any class within the class and it’s child class. then these constant, property and method have to be declared as protected. Let’s get a better understanding by this example:
<?php class GrandFather { protected $name='Masud Alam'; // A Protected variable public function getName(){ return $this->name; } } class Daddy extends GrandFather // Inherited class { function displayGrandFaName() { return $this->name; // The Protected Property will be available to the inherited class } } $grandFa=new GrandFather; echo $grandFa->getName(),"\n"; //Prints 'Masud Alam' // Inherited class Daddy wants to know Grandpas Name $daddy = new Daddy; echo $daddy->displayGrandFaName(),"\n"; // Prints 'Masud Alam' // Protected Property can not be accessed outside of the class! $outsiderWantstoKnowGrandpasName = new GrandFather; echo $outsiderWantstoKnowGrandpasName->name; // // Results in a Fatal Error ?>
Explanation: Observe, as our GrandFather class’s $name property is protected, we were able to access the $name property within the current class and it’s child class. But we were not able to access the $name property outside of the class or outside of it’s child class. If we do we will see a fatal error.
Private :
If we want to use any property, method and constant of any class only within the class itself and not in it’s child class or anywhere else. then these constant, property and method have to be declared as private. Let’s get a better understanding by this example:
<?php class GrandFather { private $name='Masud Alam'; // A Protected variable public function getName(){ return $this->name; } } class Daddy extends GrandFather // Inherited class { function displayGrandFaName() { return $this->name; // The Private Property will not available to the inherited class and will show notice } } $grandFa=new GrandFather; echo $grandFa->getName(),"\n"; //Prints 'Masud Alam' echo $gradFa->name, "\n"; //Results in a Notice // Inherited class Daddy wants to know Grandpas Name $daddy = new Daddy; echo $daddy->displayGrandFaName(),"\n"; // Results in a Notice // Protected Property can not be accessed outside of the class! $outsiderWantstoKnowGrandpasName = new GrandFather; echo $outsiderWantstoKnowGrandpasName->name; // // Results in a Fatal Error ?>
Explanation: Observe, as our GrandFather class’s $name property is private, we were able to access the $name property only within the current class. But we were not able to access the $name property anywhere else. If we try to access it anywhere else we will see an error.

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first Fifteen years a number of leadership positions at AmarBebsha Ltd as a CTO, Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now I’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.
nice explanation. I easily to understand this coocept
Nice explanation .
very nice, Sir.
And thank you sir. May Allah accept your these service as “Sadaqah Jariyah”.