Lesson content
Read, practise, then check your understanding
__construct establishes invariants and PHP 8 promotion can declare properties from parameters. Throwing prevents invalid construction.
Practical example
<?php
final class UserService {
public function __construct(private readonly PDO $database, private readonly LoggerInterface $logger) {}
}
__destruct timing/order can be unreliable, especially during shutdown. Do not depend on it for commits, critical flushes, or locks; use explicit cleanup and finally. Avoid heavy I/O or publishing $this during construction.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.