Encapsulation is one of the fundamental principles of Object-Oriented Programming (OOP). It helps in restricting direct access to an object’s properties and methods, ensuring better control over data. In PHP, Access Modifiers are used to implement encapsulation.
What Are Access Modifiers?
Access Modifiers define the visibility of class properties and methods. PHP provides three types of access modifiers:
- Public: Accessible from anywhere (inside and outside the class).
- Protected: Accessible only within the class and by derived (child) classes.
- Private: Accessible only within the class where it is declared.
Let’s explore each of these in detail with examples.
1. Public Access Modifier
A property or method declared as public
can be accessed from anywhere.
Example:
class Car {
public $brand;
public function setBrand($brand) {
$this->brand = $brand;
}
}
$car = new Car();
$car->brand = "Toyota"; // Accessible outside the class
$car->setBrand("Honda"); // Accessible outside the class
echo $car->brand; // Output: Honda
Here, the $brand
property and setBrand()
method are public, so they can be accessed and modified directly.
2. Protected Access Modifier
A protected
property or method can only be accessed within the class itself and in derived (child) classes.
Example:
class Vehicle {
protected $type;
protected function setType($type) {
$this->type = $type;
}
}
class Bike extends Vehicle {
public function setBikeType($type) {
$this->setType($type); // Accessible inside derived class
}
}
$bike = new Bike();
$bike->setBikeType("Sports"); // Allowed
// $bike->setType("Sports"); // Error: Cannot access protected method
Here, $type
and setType()
are protected, so they cannot be accessed directly from an object but can be accessed within a child class.
3. Private Access Modifier
A private
property or method can only be accessed within the class itself. It cannot be inherited or accessed from outside the class.
Example:
class User {
private $password;
public function setPassword($password) {
$this->password = $password;
}
public function getPassword() {
return $this->password; // Accessible within the class
}
}
$user = new User();
$user->setPassword("secret");
// echo $user->password; // Error: Cannot access private property
echo $user->getPassword(); // Output: secret
Here, $password
is private, meaning it can only be accessed through methods inside the class (setPassword()
and getPassword()
).
Using Getters and Setters (Encapsulation in Action)
Encapsulation ensures that object properties are not directly accessed. Instead, we use getter and setter methods to control how data is retrieved and modified.
Example:
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(500);
// echo $account->balance; // Error: Cannot access private property
echo "Balance: " . $account->getBalance(); // Output: Balance: 500
Here, balance
is private, ensuring that it cannot be directly modified from outside. Instead, we use deposit()
to add money and getBalance()
to retrieve the balance.
Summary of Access Modifiers
Modifier | Scope |
---|---|
public | Accessible anywhere (inside & outside the class) |
protected | Accessible within the class and in derived classes |
private | Accessible only within the class where it is declared |
Encapsulation helps in maintaining data integrity and restricting unintended modifications, making your PHP code more secure, maintainable, and scalable.
Next Steps:
- Learn about abstract classes and interfaces in PHP OOP.
- Explore traits and static properties/methods.
- Implement real-world encapsulation in a Laravel application.
I hope this article helps you understand Access Modifiers and Encapsulation in PHP OOP. If you have any questions, feel free to comment below! 🚀