PHP callable Keyword
Example
Use callable to require a callback function as an argument:
<?php
function printFormatted(callable $format, $str) {
echo $format($str);
echo "<br>";
}
function exclaim($str) { return $str . "!"; }
printFormatted("exclaim", "Hello World");
?>
Try it Yourself »
Definition and Usage
The callable
keyword is used to force a function argument to be a reference to a
function.
A callable can be one of the following:
- An anonymous function
- A string containing the name of a function
- An array describing a static class method
- An array describing an object method
More Examples
Example
Using different types of callables:
<?php
function printFormatted(callable $format, $str) {
echo
$format($str);
echo "<br>";
}
class MyClass {
public static function
ask($str) {
return $str . "?";
}
public function brackets($str) {
return "[$str]";
}
}
// An anonymous function
$func = function($str)
{ return substr($str, 0, 5); };
printFormatted($func , "Hello World");
// A string containing the name of a function
printFormatted("strtoupper", "Hello World");
// An array describing a
static class method
printFormatted(["MyClass", "ask"], "Hello World");
// An array describing an object method
$obj = new MyClass();
printFormatted([$obj, "brackets"], "Hello World");
?>
Try it Yourself »
❮ PHP Keywords
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.