functions
function add(int $a, int $b): int {
return $a + $b;
}
$double = fn(int $n): int => $n * 2;
Both forms were executed
Signatures
Types on parameters and return values turn mistakes into immediate errors
With strict_types=1 the coercion rules stop being surprising
Default and variadic
function scan(string $host, int $timeout = 5, string ...$extra): array {
return [$host, $timeout, $extra];
}
Defaults reduce call site noise Variadic parameters collect the rest into an array
Arrow functions
fn captures outer variables automatically
Use them for short transformations
Use a named function when the body needs several statements
Callables
First class callable syntax turns a function into a value
PHP 8.6 compiles loops for array_map with these callbacks
That is a performance detail, not a reason to contort code
Next
Objects in 07 objects
Posted