<?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]";
}
}
// 無名関数
$func = function($str) { return substr($str, 0, 5); };
printFormatted($func , "Hello World");
// 関数名を含む文字列
printFormatted("strtoupper", "Hello World");
// 静的クラスメソッドを表す配列
printFormatted(["MyClass", "ask"], "Hello World");
// オブジェクトメソッドを表す配列