The callable
type in PHP refers to anything that can be "called". This means that it can be any of the following:
- String function name;
- Anonymous function (or
Closure
); - Invokable object;
- String static class method name;
- Callable array.
String Function Name
A function's name as a string is considered a callable
. For example:
function foo() { echo 'foo'; } function run(callable $callback) { $callback(); } run('foo'); // 'foo' var_dump(is_callable('foo')); // bool(true)
You can of course pass namespaced function names as well. In that case, the string must contain the fully-qualified name of the function. For example:
function run(callable $callback) { $callback(); } namespace Helper\Output; function foo() { echo 'foo'; } run('Helper\Output\foo'); // 'foo' var_dump(is_callable('Helper\Output\foo')); // bool(true)
Closure
An instance of the Closure
object (or an anonymous function) is considered a callable
. For example:
$anonFunc = function () { echo 'foo'; }; var_dump($anonFunc); // object(Closure)#1 (0) { } function run(callable $callback) { $callback(); } run($anonFunc); // 'foo' var_dump(is_callable($anonFunc)); // bool(true)
Similarly, you can also pass in an arrow function like so:
$arrowFunc = fn () => 'foo'; var_dump($arrowFunc); // object(Closure)#1 (0) { } function run(callable $callback): string { return $callback(); } echo run($arrowFunc); // 'foo' var_dump(is_callable($arrowFunc)); // bool(true)
Invokable Object
A class that implements the __invoke()
magic method is considered a callable
. For example:
class Foo { public function __invoke() { echo 'foo'; } } function run(callable $callback) { return $callback(); } $foo = new Foo(); run($foo); // 'foo' var_dump(is_callable($foo)); // bool(true)
String Static Class Method Name
For static
methods, a string in the format 'ClassName::methodName'
is considered a callable
. For example:
class Foo { public static function staticMethod() { echo 'foo'; } } function run(callable $callback) { return $callback(); } run('Foo::staticMethod'); // 'foo' var_dump(is_callable('Foo::staticMethod')); // bool(true)
Callable Array
You can create a callable
array for an instantiated object by adding the object itself at index 0
of the array and the method name at index 1
. For example:
class Foo { public function method() { echo 'foo'; } } function run(callable $callback) { return $callback(); } $callable = [new Foo(), 'method']; run($callable); // 'foo' var_dump(is_callable($callable)); // bool(true)
You can do the same for a static
class method. However, you do not necessarily need to specify the object instance at index 0
. In this case, you may specify a class name instead as well. For example:
class Foo { public static function staticMethod() { echo 'foo'; } } function run(callable $callback) { return $callback(); } run(['Foo', 'staticMethod']); // 'foo' var_dump(is_callable(['Foo', 'staticMethod'])); // bool(true) run([new Foo(), 'staticMethod']); // 'foo' var_dump(is_callable([new Foo(), 'staticMethod'])); // bool(true)
This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.