Lesson content
Read, practise, then check your understanding
Classes group state and behavior. Keep properties private or readonly where possible and expose operations that preserve valid state.
Practical example
<?php
final class Percentage {
public function __construct(public readonly float $value) {
if ($value < 0 || $value > 100) throw new InvalidArgumentException('0..100 required');
}
public function asRatio(): float { return $this->value / 100; }
}
$this is the receiver. Static mutable state hides dependencies and complicates long-running workers. Magic methods should remain unsurprising. Prefer small cohesive objects, explicit collaborators, and value objects for stable domain concepts.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.