Introduction
Encapsulation is one of the core principles of Object-Oriented Programming (OOP). It refers to the concept of restricting direct access to certain parts of an object and allowing controlled access through methods.
In simple words, encapsulation means data hiding.
Instead of allowing users to directly access and modify object properties, we protect them using access modifiers like private and provide public methods to interact with them safely.
In this tutorial, you will learn what encapsulation is, why it is important, and how to implement it in PHP.
What is Encapsulation?
Encapsulation is the process of wrapping data (properties) and methods (functions) together inside a class and restricting direct access to some of the object’s components.
It ensures that:
- Data cannot be modified directly from outside the class
- Access is controlled through public methods
- Internal implementation is hidden
Encapsulation improves security, structure, and reliability of your code.
Why Encapsulation is Important?
Encapsulation helps you:
- Protect sensitive data
- Prevent accidental changes
- Maintain control over object behavior
- Improve code security
- Make code easier to maintain
- Implement secure programming practices
Problem Without Encapsulation
<?php
class User {
public $password;
}
$user = new User();
$user->password = "123456"; // ❌ Not secure
?>
Anyone can change the password directly — this is unsafe.
Encapsulation Solution (Using Private Property)
<?php
class User {
private $password;
public function setPassword($pass) {
$this->password = $pass;
}
public function getPassword() {
return $this->password;
}
}
?>
Using Encapsulated Class
<?php
$user = new User();
$user->setPassword("securePass123");
echo $user->getPassword();
?>
✔ Password is protected
✔ Access is controlled
Adding Validation in Setter
Encapsulation allows you to validate data.
<?php
class User {
private $password;
public function setPassword($pass) {
if (strlen($pass) < 6) {
echo "Password must be at least 6 characters";
return;
}
$this->password = $pass;
}
}
?>
Real-World Example
Think of an ATM Machine:
- You cannot access cash directly
- You must use buttons (methods)
- System validates PIN internally
This is encapsulation.
Encapsulation + Inheritance
private→ not accessible in child classprotected→ accessible in child class
class User {
protected $email;
}
Example Without Encapsulation
<?php
class BankAccount {
public $balance;
}$account = new BankAccount();
$account->balance = -5000;echo $account->balance;
?>
Problem
Here, anyone can set the balance to a negative value.
There is no control or validation.
This is not safe.
Example With Encapsulation
<?php
class BankAccount {
private $balance = 0; public function deposit($amount) {
if ($amount > 0) {
$this->balance += $amount;
}
} public function getBalance() {
return $this->balance;
}
}$account = new BankAccount();
$account->deposit(1000);echo $account->getBalance();
?>
Explanation
$balanceis private- It cannot be accessed directly
deposit()controls how balance is modifiedgetBalance()safely returns the value
Now, the balance cannot be set to invalid values directly.
This is encapsulation in action.
What Are Getters and Setters?
Getters and setters are public methods used to access and modify private properties.
- Getter → Used to retrieve value
- Setter → Used to update value with validation
Example:
public function setAge($age) {
if ($age > 0) {
$this->age = $age;
}
}public function getAge() {
return $this->age;
}
This ensures invalid data cannot enter the system.
Common Beginner Mistakes
- Making all properties public
- Not using validation inside setters
- Forgetting to create getter methods
- Accessing private properties directly
Best practice:
Keep properties private unless necessary.
Best Practices
- Keep properties
private - Expose behavior via public methods
- Validate data inside setters
- Avoid public properties in real projects
Common Beginner Mistakes
- Making all properties public
- Not using validation inside setters
- Forgetting to create getter methods
- Accessing private properties directly
Best practice:
Keep properties private unless necessary.
Practical Task
Create a class called User:
- Add a private property called
email - Create a setter method that checks if email contains “@”
- Create a getter method to return the email
- Try setting an invalid email and observe the behavior
Summary
- Encapsulation means data hiding
- It protects object properties from direct access
- Implemented using private properties
- Getter and setter methods control access
- It improves security and structure
Encapsulation is a fundamental OOP concept and is essential for writing safe and maintainable PHP code.
In the next tutorial, we’ll learn about Polymorphism in PHP.
