Introduction
Encapsulation is one of the most important concepts in Object-Oriented Programming (OOP). In PHP, encapsulation means wrapping data (variables) and methods (functions) together into a single unit called a class and controlling how that data is accessed.
In simple words, encapsulation is the process of hiding the internal details of an object and allowing access only through specific methods. This helps protect the data from being modified directly from outside the class.
Encapsulation mainly works with access modifiers such as:
- private
- protected
- public
These modifiers help control who can access the data or methods inside a class.
Example in Real Life
1. Capsule Medicine Example:
A capsule contains multiple medicines wrapped inside a single cover. We only consume the capsule without knowing what is inside. This is similar to encapsulation where the internal implementation is hidden.
2. Television Example:
A TV contains many internal circuits and components. However, we control the TV using a remote control without opening the TV. The internal system is hidden from users, which is a perfect example of encapsulation.
Syntax
In PHP, encapsulation is implemented using private variables and public methods (getter and setter methods).
Basic syntax:
class ClassName {
private $variable;
public function setValue($value) {
$this->variable = $value;
}
public function getValue() {
return $this->variable;
}
}
Explanation of syntax
- private $variable → The variable cannot be accessed directly from outside the class.
- setValue() → Used to assign a value to the private variable.
- getValue() → Used to retrieve the value.
This approach keeps the data secure and controlled.
Example
class Student {
private $name;
public function setName($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
$obj = new Student();
$obj->setName("John");
echo $obj->getName();
Output:
This example demonstrates how encapsulation works by keeping the variable private and accessing it through methods.
Real-world Example
Let us consider a Bank Account system where the balance should not be directly accessed or modified by users.
class BankAccount {
private $balance = 0;
public function deposit($amount) {
$this->balance = $this->balance + $amount;
}
public function withdraw($amount) {
if($amount balance) {
$this->balance = $this->balance - $amount;
} else {
echo "Insufficient Balance
";
}
}
public function getBalance() {
return $this->balance;
}
}
$account = new BankAccount();
$account->deposit(1000);
$account->withdraw(300);
echo "Current Balance: " . $account->getBalance();
This example protects the balance variable by making it private, ensuring that users cannot directly change it.
Output:
If the user tries to withdraw more money than available, the program will show:
Explanation:
Let us understand how the code works step by step.
First, we create a class named BankAccount. Inside the class, we declare a private variable $balance. Because it is private, it cannot be accessed directly from outside the class.
private $balance = 0;
Next, we create a deposit method:
public function deposit($amount)
This method allows users to add money to the account. It updates the balance safely inside the class.
Then we create a withdraw method:
public function withdraw($amount)
This method checks if the withdrawal amount is less than or equal to the available balance. If yes, the amount is deducted. Otherwise, it displays an error message.
Finally, we create a getter method:
public function getBalance()
This method returns the current balance to the user without exposing the variable directly.
When the object $account is created, we call the methods:
$account->deposit(1000);
$account->withdraw(300);
This updates the balance internally.
Finally, the balance is displayed using:
echo $account->getBalance();
This approach ensures that data remains protected and only authorized methods can modify it.
Conclusion
Encapsulation is a fundamental concept of Object-Oriented Programming in PHP. It allows developers to protect data by hiding internal implementation details and providing controlled access through methods.
By using access modifiers such as private, protected, and public, developers can ensure that sensitive data cannot be modified directly from outside the class.
Related PHP Tutorials
PHP Encapsulation – Interview Questions
Q 1: What is encapsulation in PHP?
Q 2: How is encapsulation achieved?
Q 3: Why is encapsulation important?
Q 4: What are getter and setter methods?
Q 5: Is encapsulation an OOP principle?
PHP Encapsulation – Objective Questions (MCQs)
Q1. Encapsulation in PHP is achieved using ______.
Q2. Encapsulation helps in ______.
Q3. Private properties can be accessed using ______.
Q4. Encapsulation improves ______.
Q5. Which of the following demonstrates encapsulation?