Introduction
In Object-Oriented Programming, access modifiers control the visibility of properties and methods inside a class.
They determine:
- Who can access a property
- Who can call a method
- Whether data can be modified from outside the class
In PHP, there are three main access modifiers:
publicprivateprotected
Understanding access modifiers is important because they help protect data and improve code security and structure.
Why Are Access Modifiers Important?
Without access control, anyone could change the internal data of an object. This could lead to unexpected behavior and security problems.
Access modifiers help you:
- Protect sensitive data
- Control how properties are accessed
- Prevent accidental changes
- Follow proper OOP principles
- Implement encapsulation
They make your code safer and more professional.
Public Access Modifier
What is public?
- Accessible from anywhere
- Can be used inside and outside the class
Public Example
<?php
class User {
public $name;
public function setName($name) {
$this->name = $name;
}
}
?>
Accessing Public Members
<?php
$user = new User();
$user->name = "Divyesh";
$user->setName("Divyesh");
?>
✔ Works fine
✔ No restriction
Private Access Modifier
What is private?
- Accessible only inside the same class
- Cannot be accessed from outside or child classes
Used to hide sensitive data.
Private Example
<?php
class User {
private $password;
public function setPassword($pass) {
$this->password = $pass;
}
}
?>
Trying to Access Private Property
<?php
$user = new User();
$user->password = "123456"; // ❌ Error
?>
❌ This will cause an error because $password is private.
Correct Way (Using Method)
<?php
$user->setPassword("123456");
?>
✔ Secure access
Why Private is Important (Encapsulation)
Encapsulation means hiding internal details and allowing access only through controlled methods.
For example:
- You should not allow users to directly change account balance
- Instead, provide deposit() and withdraw() methods
This prevents invalid or harmful data changes.
Protected Access Modifier
What is protected?
- Accessible inside the class
- Accessible in child (inherited) classes
- Not accessible from outside
Protected Example
<?php
class User {
protected $role = "User";
}
?>
Child Class Access
<?php
class Admin extends User {
public function getRole() {
return $this->role;
}
}
$admin = new Admin();
echo $admin->getRole();
?>
✔ Works because role is protected.
Outside Access
<?php
$admin = new Admin();
echo $admin->role; // ❌ Error
?>
❌ Not allowed
Difference Between Access Modifiers
| Modifier | Same Class | Child Class | Outside Class |
|---|---|---|---|
| public | ✅ | ✅ | ✅ |
| protected | ✅ | ✅ | ❌ |
| private | ✅ | ❌ | ❌ |
Why Access Modifiers Matter
They help you:
- Prevent accidental data modification
- Secure sensitive information
- Control how your class is used
- Follow encapsulation principle
Real-World Analogy
Think of a company:
- Public → Information available to everyone
- Private → Personal data accessible only by the owner
- Protected → Data accessible within the company hierarchy
This is how access control works in OOP as well.
Common Beginner Mistakes
- Forgetting to declare visibility (PHP requires it)
- Trying to access private properties directly
- Confusing protected with private
- Not using getter and setter methods
- Making everything public
A good practice is:
👉 Keep properties private
👉 Use public methods to control access
Practical Task
Create a class named BankAccount:
- Add a private property called
balance - Create a public method to deposit money
- Create a public method to withdraw money
- Try accessing
balancedirectly and observe what happens
This will help you understand access control better.
Summary
- Access modifiers control visibility in PHP classes
publicallows access everywhereprivateallows access only inside the classprotectedallows access inside the class and child classes- They help implement encapsulation and protect data
Access modifiers are essential for writing secure and structured OOP code.
In the next tutorial, we’ll learn about Inheritance in PHP.
