Problem Statement
Explain encapsulation with a simple example in PHP.
Explanation
Encapsulation means binding data and methods together while restricting direct access to some data. It is achieved using private and protected access modifiers. This helps maintain control over how data is accessed or changed.
Example:
class BankAccount {
private $balance = 0;
public function deposit($amount){ $this->balance += $amount; }
public function getBalance(){ return $this->balance; }
}
Code Solution
SolutionRead Only
class BankAccount { private $balance; }