Mastering PHP Access Modifiers: public, private, and protected Explained
Last updated 3 months, 4 weeks ago | 340 views 75 5

Introduction: Why PHP Access Modifiers Matter
In object-oriented programming, encapsulation is key to writing clean, secure, and maintainable code. PHP access modifiers help you control how and where your class members (properties and methods) can be accessed.
Without access modifiers, anyone can access and modify any part of your object — which can break your application’s integrity or expose sensitive logic.
That’s where public, private, and protected come in.
What Are Access Modifiers in PHP?
Access modifiers define the visibility scope of class properties and methods. In PHP, there are three main types:
Modifier | Scope (Where it's accessible) |
---|---|
public |
Accessible from anywhere |
protected |
Accessible within the class and by child classes only |
private |
Accessible only within the class where it is defined |
Step-by-Step: PHP Access Modifiers in Action
1. Public Modifier
The public
keyword makes the member fully accessible—from anywhere: inside the class, outside the class, and even from inherited classes.
class Car {
public $brand = "Toyota";
public function getBrand() {
return $this->brand;
}
}
$car = new Car();
echo $car->brand; // Output: Toyota
2. Private Modifier
The private
keyword restricts the access to the defining class only.
class BankAccount {
private $balance = 1000;
public function getBalance() {
return $this->balance;
}
}
$acc = new BankAccount();
echo $acc->getBalance(); // Output: 1000
// echo $acc->balance; // ❌ Error: Cannot access private property
3. Protected Modifier
The protected
keyword allows access within the class and any subclass, but not from outside.
class Person {
protected $name = "John";
}
class Student extends Person {
public function getName() {
return $this->name;
}
}
$stu = new Student();
echo $stu->getName(); // Output: John
// echo $stu->name; // ❌ Error: Cannot access protected property
Full Working Example
<?php
class Employee {
public $publicInfo = "Available to all";
protected $department = "Engineering";
private $salary = 50000;
public function showDetails() {
echo "Public: $this->publicInfo<br>";
echo "Protected: $this->department<br>";
echo "Private: $this->salary<br>";
}
}
class Manager extends Employee {
public function showManagerInfo() {
echo "Accessing protected in subclass: $this->department<br>";
// echo $this->salary; // ❌ Not allowed: private in parent
}
}
$emp = new Employee();
$emp->showDetails();
echo $emp->publicInfo; // ✅ Accessible
// echo $emp->department; // ❌ Error
// echo $emp->salary; // ❌ Error
$mgr = new Manager();
$mgr->showManagerInfo();
?>
Tips & Common Pitfalls
✅ Best Practices
-
Use
private
for sensitive data (e.g., passwords, tokens). -
Use
protected
when expecting inheritance but not outside access. -
Use
public
only for data meant to be universally accessible. -
Always use getter/setter methods to expose private/protected data if needed.
❌ Common Mistakes
-
Overusing
public
, which can lead to unintentional data leaks. -
Accessing
protected
orprivate
members directly from outside. -
Forgetting that private members aren’t accessible in child classes.
Comparison Table: Access Modifiers
Access Modifier | Within Class | In Subclass | Outside Class |
---|---|---|---|
public |
✅ Yes | ✅ Yes | ✅ Yes |
protected |
✅ Yes | ✅ Yes | ❌ No |
private |
✅ Yes | ❌ No | ❌ No |
Conclusion: Choose Access Modifiers Wisely
PHP access modifiers let you control how your class internals are accessed, leading to more secure and modular code. Choosing the right modifier ensures data safety, improves code structure, and prepares your code for scalability.
Actionable Takeaways:
-
Use
private
for sensitive logic and values -
Use
protected
to allow subclass access -
Use
public
sparingly and only when necessary -
Rely on getter/setter patterns for controlled access