Type declarations in PHP migliorano la robustezza del codice.
Parameter types
function greet(string $name): string {
return "Hello, $name!";
}
function sum(int $a, int $b): int {
return $a + $b;
}
function process(array $items): void {
foreach ($items as $item) {
// ...
}
}Nullable types
function find(?int $id): ?User {
if ($id === null) return null;
return User::find($id);
}Union types (PHP 8+)
function format(int|float $number): string {
return number_format($number, 2);
}Mixed e never (PHP 8+)
function handle(mixed $input): mixed { }
function error(): never {
throw new Exception();
}