Access Modifiers in PHP: Public, Protected, Private
If you are learning object-oriented programming (OOP) in PHP, it is essential to understand the concepts of access modifiers.
Basically, access modifiers in PHP are keywords that are used to control the visibility and accessibility of class properties and methods. They define where and how class members can be accessed—whether inside the same class, by derived (child) classes, or from outside the class.
Access modifiers also play an important role in validating and restricting information within a program. For example, your credit card number and its PIN are private because you do not want anyone else to access them.
If such sensitive information were declared as public, anyone could view your credit card details and PIN, which would be unsafe.
Types of Access Modifiers in PHP
PHP supports three types of access modifiers:
- public
- protected
- private
Let’s explore each one in detail with the help of examples.
Public Access Modifier in PHP
When you declare properties and methods as public in the class, you can access them from anywhere:
- Inside the class
- Outside the class
- From subclass (child or derived) classes
The general syntax for declaring properties and methods as public is as follows:
public $property;
public function method() { }
Basic Example:
<?php
class Student {
// Declaration of property and method as public.
public $name;
public function showName() {
echo $this->name;
}
}
// Create an instance of class Student.
$stu = new Student();
// Assigning value to the public property.
$stu->name = "Amit"; // Accessible outside
$stu->showName(); // Calling a public method
?>
Output:
Amit
In this example, we have created a class named Student, which contains a public variable $name and a public method showName(). Since both are declared as public, you can access them from anywhere: inside the class, outside the class, and from objects of the class.
Inside the method, $this->name refers to the name property of the current object. Since the variable $name is public, we can directly assign a value from outside the class.
Protected Access Modifier in PHP
When you declare properties or methods as protected, you can access them from:
- Inside the same class
- Inside child (derived) classes
You cannot access them from outside the class.
The general syntax for declaring properties and methods as protected is as:
protected $property;
protected function method() { }
Example 1: Accessing Protected Property from Inside and Outside Class
<?php
class Person {
protected $age = 25;
public function display(){
// Accessing protected property from inside the class.
echo "Age: " .$this->age; // Valid
}
}
$obj = new Person();
$obj->display();
// echo $obj->age; // Error: Cannot access protected property from outside the class.
?>
Output:
Age: 25
Example 2: Protected Modifier with Inheritance
<?php
class Person {
protected $name = "Saanvi";
protected $age = 25;
public function display(){
echo "Name: " . $this->name ."\n";
}
}
class Employee extends Person {
public function showAge() {
return $this->age;
}
}
$emp = new Employee();
$emp->display();
echo "Age: " .$emp->showAge();
?>
Output:
Name: Saanvi Age: 25
Private Access Modifier in PHP
When you declare properties or methods as private, you can access them only within the same class. They are not accessible from:
- Outside the class
- By derived (child) classes
The general syntax for declaring properties or methods with private modifier is as:
private $property;
private function method() { }
Example:
<?php
class Account {
private $balance = 5000;
public function getBalance() {
return $this->balance;
}
}
$obj = new Account();
echo "Available balance: " . $obj->getBalance();
echo $obj->balance; // Error: Cannot access private property from outside.
?>
Output:
Available balance: 5000
As you can see in this example, any method inside the same class has access to private property (like $balance). But, you cannot access it from outside the class.
PHP Access Modifiers Comparison Table
Here, you can see the comparison table of access modifiers in PHP:
| Modifier | Same Class | Child Class | Outside Class |
|---|---|---|---|
| public | ✔ Yes | ✔ Yes | ✔ Yes |
| protected | ✔ Yes | ✔ Yes | ✘ No |
| private | ✔ Yes | ✘ No | ✘ No |
Best Practices for Using Access Modifiers in PHP
Following are the key points of using access modifiers in PHP that you should keep in mind:
- Use private modifier for sensitive data or information.
- Use protected modifier when you are designing extensible classes.
- Use public modifier only for necessary methods.
- Avoid exposing class fields directly.
- Use getter and setter methods for controlled access.
Conclusion
Access modifiers in PHP control the visibility and accessibility of class members, such as properties (variables) and methods (functions). They help promote better code organization, security, and maintainability.
We hope you have understood how to use the public, protected, and private access modifiers to control accessibility inside and outside the class, and practiced all the examples provided.

