What it is and why to use it
A helper can define the overall work while letting its caller provide one transformation. A callable parameter accepts a value that can be invoked, such as a closure, a suitable function name, a method reference or an object with __invoke(). This is the contract behind many callback-based APIs, including array_map and usort.
How it works
Declare callable on a parameter and invoke its variable with parentheses. The callable type ensures the supplied value can be called in the relevant context, but does not declare the callback’s full parameter and return signature. Document that signature and validate results where necessary. callable is not a permitted property type; use Closure when a typed property must store a closure. First-class callable syntax is an additional PHP 8.1 feature and is not needed by these PHP 8.0-compatible examples.
Common mistakes and limitations
Never turn an arbitrary request parameter into a function or method call. Keep supported behaviours under application control. Do not assume every string naming a function is automatically appropriate or authorised. Use accessible object methods and avoid deprecated context-dependent callable forms.
Run the examples locally
Save each complete example in its own example.php file and run php example.php in a terminal from that folder. Each example is independent: do not paste both into one file, especially when they declare classes with the same names. Output appears in the terminal. The examples use PHP 8.0-compatible syntax; use a currently supported PHP release for development.
The <?php opening tag starts PHP code. declare(strict_types=1) requests strict scalar argument checks for function calls made from this file; it does not validate external input for you. echo writes output, and PHP_EOL adds the platform’s line ending. Quotation marks delimit strings; they are not part of the printed text unless explicitly included in the string.
These examples use built-in PHP language features and standard extensions. They need no Composer packages, database server, browser APIs or network access. Codectionary’s PHP preview does not execute PHP; use your local interpreter to reproduce the output.
Check your interpreter, lint and run one saved example
php -v
php -l example.php
php example.phpContinue learning
Use the related guides to connect this operation to the surrounding language features. Practise by changing the example inputs and predicting the output before running it.
Syntax
function transform(string $value, callable $callback): string { return $callback($value); }Examples
Pass a transformation into a helper
Use a closure to customise one step in a reusable formatting operation.
<?php
declare(strict_types=1);
function label(string $value, callable $transform): string {
return '[' . $transform($value) . ']';
}
echo label('php', fn(string $value): string => strtoupper($value)), PHP_EOL;
- label owns the bracket formatting and accepts a callable for transforming the inner text.
- The arrow function accepts a string and returns its ASCII uppercase form for this fixed sample.
- Calling the variable runs that function and the helper wraps its result. The callable contract itself does not express the string-to-string signature, so document it for real callers.
- Expected output: [PHP]
Use an invokable object with configuration
Store a prefix in an object that can be passed like a function.
<?php
declare(strict_types=1);
class Prefixer {
public function __construct(private string $prefix) {}
public function __invoke(string $value): string {
return $this->prefix . $value;
}
}
function apply(string $value, callable $formatter): string {
return $formatter($value);
}
$formatter = new Prefixer('Learn ');
echo apply('PHP', $formatter), PHP_EOL;
- Prefixer stores configuration in a private property and exposes __invoke as its call operation.
- The instance satisfies the callable parameter accepted by apply.
- Invoking formatter uses its stored prefix. This supports small configured strategies without relying on global variables.
- Expected output: Learn PHP
Best practices
- Document the callback’s expected inputs and result.
- Select callbacks from trusted application code rather than user-supplied names.
Runtime & versions
PHP runtime on a server or CLI. Feature availability depends on the language and runtime version used by your project; consult the official reference below.
PHP Manual