Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP Visibility modes
Introduction
In PHP, it is possible to have a user-defined compund data type using class keyword. A new instance of class is an object. Charactersitics of object are as per definition of class, which may contain property, constant and method members.
Accessibility (also called visibility) of a class member depends on visibility prefix keyword attached in its definition. PHP has three visibility keywords - public, private and protected. A class member declared with public keyword is accessible from anywhare. A protected member is accessible from within its class and by inheriting class. On the other hand, a private member can only be accessed by the same class in which it is defined and is not visible to anything outside it.
Property Visibility
A property is an attribute of class instance. It may be an instance property (having different value for each object) or class property with same value for each object. A property may be of any valid PHP data type. A property has public visibility by default. If a property is defined using var keyword (which has been deprecated now), it is treated as public.
Example
<?php
class myclass{
public $fname="Ajay";
var $lname; //treated as public
private $marks=100;
protected $age=20;
}
$obj=new myclass();
echo "$obj->fname
";
$obj->lname="Diwan";
echo "$obj->marks
";
$obj->age=21;
?>
Output
Following output shows that public properties can be accessed outside class, while private and protected propeties throw uncaught errors
Ajay PHP Fatal error: Uncaught Error: Cannot access private property myclass::$marks PHP Fatal error: Uncaught Error: Cannot access protected property myclass::$age
However, private and protected properties can be used inside function in the same class
Example
<?php
class myclass{
public $fname="Ajay";
var $lname;
private $marks=100;
protected $age=20;
function displaydata(){
$this->lname="Diwan";
echo "$this->fname $this->lname
";
echo "marks=$this->marks age=$this->age";
}
}
$obj=new myclass();
$obj->displaydata();
?>
Output
This will produce following result. −
Ajay Diwan marks=100 age=20
Method Visibility
Like class properties, class methods can also be assigned visibility with public, private and protected keywords. Methods too, by default are treated as public.
Example
<?php
class myclass{
public $fname="Ajay";
var $lname;
private $marks=100;
protected $age=20;
public function setname(){
$this->fname="Anil";
$this->lname="Dave";
echo "name changed
";
}
private function setmarks(){
$this->marks=90;
}
protected function setage(){
$this->age=21;
}
function setdata(){
$this->setname();
$this->setmarks();
$this->setage();
}
function displaydata(){
$this->lname="Diwan";
echo "$this->fname $this->lname
";
echo "marks=$this->marks age=$this->age";
}
}
$obj=new myclass();
$obj->setname();//works
$obj->setmarks();//error
$obj->setage();/error
$obj->setdata(); //private and protected methods called withing public method
$obj->displaydata();
?>
Output
This will produce following result. −
name changed PHP Fatal error: Uncaught Error: Call to private method myclass::setmarks() from context '' PHP Fatal error: Uncaught Error: Call to protected method myclass::setage() from context '' Anil Dave marks=90 age=21
Method Visibility in Inheritance
Parent class methods can be overridden in child class definition. Private and protected methods of parent are not available for object of child class. A method that is protected in parent can however be used in a child class method
Example
<?php
class testclass{
public $x=10;
private $y=20;
protected $z=30;
private function test(){
echo "testclass private method
";
}
public function test1(){
echo "testclass public method
";
}
protected function test2(){
echo "testclass protected method
";
}
function test3(){
echo "x=$this->x y=$this->y z=$this->z
";
}
}
class newclass extends testclass{
function test1(){
echo "newclass public method
";
}
public function test4(){
$this->test();
$this->test1();
$this->test2();
}
}
$obj=new newclass();
echo $obj->test1();
echo $obj->test3();
echo $obj->test4();
?>
Output
This will show following result
newclass public method x=10 y=20 z=30 PHP Fatal error: Uncaught Error: Call to private method testclass::test() from context 'newclass'
Constant Visibility
From PHP 7.1 onwards, visibility modes can be used with constaants. Default visibility of constant is public
Example
<?php
class testclass{
public const X=10;
private const Y=20;
protected const Z=30;
function showconst(){
echo "x=" . self::X ;
echo "y=" . self::Y;
echo "z=" . self::Z ;
}
}
$obj=new testclass();
$obj->showconst();
echo testclass::Y . "
";
echo testclass::Z . "
";
?>
Output
This will show following result
x=10y=20z=30 PHP Fatal error: Uncaught Error: Cannot access private const testclass::Y PHP Fatal error: Uncaught Error: Cannot access protected const testclass::Z