Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

PHP type callable

A callable parameter accepts something that PHP can invoke, such as a closure or a public method paired with an object.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

function apply(callable $transform, int $value): int

A callable parameter accepts something that PHP can invoke, such as a closure or a public method paired with an object. The declaration does not specify the argument types or return type of that callable. Invoking it still checks the target signature. Use Closure for a typed property because callable is unavailable as a property type.

class Scale { public function twice(int $n): int { return $n * 2; } }
function apply(callable $transform, int $value): int { return $transform($value); }
echo json_encode([apply([new Scale(), 'twice'], 3), apply(fn(int $n): int => $n+1, 3)]);
[6,4]

Examples

<?php
class Scale { public function twice(int $n): int { return $n * 2; } }
function apply(callable $transform, int $value): int { return $transform($value); }
echo json_encode([apply([new Scale(), 'twice'], 3), apply(fn(int $n): int => $n+1, 3)]);

Result

[6,4]

Sources