Lesson content
Read, practise, then check your understanding
Modern PHP supports scalar/class parameter and return types, nullable types, unions, intersections, mixed, void, never, static, self, and parent.
Practical example
<?php
declare(strict_types=1);
function find(int $id): User|null { return $repository->find($id); }
function normalize(int|string $value): string { return (string) $value; }
function fail(string $message): never { throw new RuntimeException($message); }
Strict types improves scalar call checks but does not replace validation. Choose types that express domain states, avoid broad mixed when a smaller union works, and use static analysis/docblock generics for collection element contracts PHP cannot fully express natively.
Knowledge check
Answer every question correctly to complete this chapter.
0 of 10 checks passed
Your progress is saved on this device.