Public, Private and Protected — a practical guide to public private protected PHP with clear examples you can reuse in real projects.
OOP Tutorial Series (14/65). Prefer one article? Read the complete OOP tutorial.
Short description
Default to private/protected for state; expose only what callers need via public methods.
Visibility example
class BankAccount {
private float $balance = 0;
public function deposit(float $amount): void {
if ($amount <= 0) throw new InvalidArgumentException('Invalid amount');
$this->balance += $amount;
}
}