Junior — Middle
What functions need to be defined for full encapsulation of a class?
sobes.tech AI
Answer from AI
To fully encapsulate a class in PHP, it is necessary to define access methods for private or protected properties — usually getters (get) and setters (set). They allow controlling the reading and modification of the object's internal data without giving direct access to the properties.
Example:
class User {
private string $name;
public function getName(): string {
return $this->name;
}
public function setName(string $name): void {
// Validation can be added here
$this->name = $name;
}
}
Thus, properties remain hidden, and access to them is through methods, which is the essence of encapsulation.