Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
PHP Visibility modes
In PHP, visibility modes control the accessibility of class members (properties, methods, and constants). PHP provides three visibility keywords: public, private, and protected. Public members are accessible from anywhere, protected members can be accessed within the class and its subclasses, while private members are only accessible within the same class.
Property Visibility
Properties are class attributes that store data. By default, properties are public. The deprecated var keyword also creates public properties ?
<?php
class MyClass {
public $fname = "Ajay";
var $lname; // treated as public (deprecated)
private $marks = 100;
protected $age = 20;
}
$obj = new MyClass();
echo $obj->fname . "
"; // Works - public property
$obj->lname = "Diwan"; // Works - public property
// echo $obj->marks; // Error - private property
// $obj->age = 21; // Error - protected property
?>
Ajay
Private and protected properties can only be accessed through class methods ?
<?php
class MyClass {
public $fname = "Ajay";
private $marks = 100;
protected $age = 20;
function displayData() {
echo "Name: $this->fname
";
echo "Marks: $this->marks
";
echo "Age: $this->age
";
}
}
$obj = new MyClass();
$obj->displayData();
?>
Name: Ajay Marks: 100 Age: 20
Method Visibility
Methods follow the same visibility rules as properties. Private and protected methods cannot be called directly from outside the class ?
<?php
class MyClass {
private $marks = 100;
public function setPublicData() {
echo "Public method called
";
}
private function setPrivateData() {
$this->marks = 90;
echo "Private method executed
";
}
public function callPrivateMethod() {
$this->setPrivateData(); // Works - called from within class
}
public function getMarks() {
return $this->marks;
}
}
$obj = new MyClass();
$obj->setPublicData();
$obj->callPrivateMethod();
echo "Marks: " . $obj->getMarks() . "
";
?>
Public method called Private method executed Marks: 90
Visibility in Inheritance
In inheritance, protected members become accessible to child classes, while private members remain hidden ?
<?php
class ParentClass {
public $x = 10;
protected $z = 30;
private $y = 20;
protected function protectedMethod() {
echo "Protected method in parent
";
}
}
class ChildClass extends ParentClass {
public function accessParentMembers() {
echo "x: $this->x
"; // Works - public
echo "z: $this->z
"; // Works - protected
// echo "y: $this->y
"; // Error - private
$this->protectedMethod(); // Works - protected method
}
}
$child = new ChildClass();
$child->accessParentMembers();
?>
x: 10 z: 30 Protected method in parent
Constant Visibility
From PHP 7.1 onwards, class constants can have visibility modifiers. Constants are public by default ?
<?php
class TestClass {
public const X = 10;
private const Y = 20;
protected const Z = 30;
public function showConstants() {
echo "X: " . self::X . "
";
echo "Y: " . self::Y . "
";
echo "Z: " . self::Z . "
";
}
}
$obj = new TestClass();
echo "Public constant: " . TestClass::X . "
";
$obj->showConstants();
// echo TestClass::Y; // Error - private constant
?>
Public constant: 10 X: 10 Y: 20 Z: 30
Visibility Summary
| Visibility | Same Class | Subclasses | Outside Class |
|---|---|---|---|
| public | ? | ? | ? |
| protected | ? | ? | ? |
| private | ? | ? | ? |
Conclusion
PHP visibility modes provide essential encapsulation features. Use public for general access, protected for inheritance hierarchies, and private for internal implementation details that should remain hidden from external code.
