Skip to content
OpenCloseEsc
Tips
  • PHP
  • syntax
  • collections

Pass methods with first-class callable syntax

strtoupper(...) and $this->toDto(...) create a Closure from a function or method, so callbacks stop being strings or arrow-function wrappers.

1 min read

Since PHP 8.1 the (...) suffix turns any callable into a Closure without calling it. It replaces 'strtoupper', [$this, 'toDto'] and fn ($x) => $this->toDto($x).

$names = array_map(strtoupper(...), $names);
 
$dtos = $users->map($this->toDto(...));
 
$handler = $service->handle(...);

Unlike the string and array forms, the IDE sees the reference, so "find usages" and refactoring work, and a typo is a compile-time error instead of a runtime "call to undefined function". Static methods take the same shape: Str::slug(...).