Encapsulation is a process of wraping/combine/bind properties and methods (behavior) in a single unit or single container.
All state are private , cannot change state. Only can change with public function.
- Information hiding
- Hide internal states and behavior from external world
- Protect the modification of state
class BankAccount {
private $balance;
public function __construct($initialBalance) {
$this->balance = $initialBalance;
}
public function deposit($amount) {
$this->balance += $amount;
}
public function withdraw($amount) {
if ($this->balance >= $amount) {
$this->balance -= $amount;
} else {
throw new Exception("Insufficient funds");
}
}
public function getBalance() {
return $this->balance;
}
}
$bankAccount = new BankAccount(1000);
// Make a deposit
$bankAccount->deposit(500);
// Make a withdrawal
try {
$bankAccount->withdraw(2000);
} catch (Exception $e) {
echo $e->getMessage(); // Output: Insufficient funds
}
echo $bankAccount->getBalance(); // Output: 1500