Data Hiding
Data hiding is a core principle of object-oriented programming that restricts direct access to certain components of an object. It is achieved through visibility modifiers: private, protected, and public.
By hiding internal state, a class controls how its data is accessed and modified, preventing accidental corruption and enforcing invariants. This leads to more maintainable, testable, and robust code.
PHP provides several mechanisms for data hiding:
- Visibility modifiers on properties and methods:
private,protected,public - The
readonlykeyword for immutable properties - The
__get()and__set()magic methods for controlled access - Typed properties which enforce type constraints on assignment
Data hiding is closely related to the concept of encapsulation, which bundles data and methods that operate on that data within a single unit, controlling external access.
<?php
class BankAccount {
private float $balance;
private string $owner;
public function __construct(string $owner, float $initialBalance) {
$this->owner = $owner;
$this->balance = $initialBalance;
}
// Controlled access through methods
public function deposit(float $amount): void {
if ($amount <= 0) {
throw new \InvalidArgumentException('Amount must be positive');
}
$this->balance += $amount;
}
public function getBalance(): float {
return $this->balance;
}
}
$account = new BankAccount('Alice', 1000);
// $account->balance = 999999; // Fatal error: cannot access private property
$account->deposit(500);
echo $account->getBalance(); // 1500
?>