更准确地说,是函数声明参数中使用的“可调用”。就像下面这个一样。
function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}
它对我们有什么好处?
我们为什么以及如何使用它?
这是我复制上述代码的链接:link
这是一个类型提示,告诉我们这个函数接受参数
$handler
作为函数,请参阅此示例以澄清事实:
function helloWorld()
{
echo 'Hello World!';
}
function handle(callable $fn)
{
$fn(); // We know the parameter is callable then we execute the function.
}
handle('helloWorld'); // Outputs: Hello World!
这是一个非常简单的例子,但我希望它可以帮助你理解这个想法。
callable
类型允许我们将回调函数传递给正在调用的函数。也就是说,回调函数参数允许被调用的函数动态调用我们在 callable
函数参数中指定的代码。 这很有用,因为它允许我们将要执行的动态代码传递给函数。
例如,人们可能想要调用一个函数,并且该函数接受一个名为
log
的回调函数,该函数将以您想要的自定义方式记录数据。
我希望这是有道理的。详情请参阅此链接。
这是使用 callable 作为参数的示例。
下面的 wait_do_linebreak 函数将休眠给定的时间,然后使用给定的尾部参数调用函数,然后回显换行符。
...$params
将尾部参数打包到名为 $params 的数组中。这里它被用来将参数代理到可调用对象中。
在示例的末尾,您将看到一个以可调用对象作为参数的本机函数。
<?php
function wait_do_linebreak($time, callable $something, ...$params)
{
sleep($time);
call_user_func_array($something, $params);
echo "\n";
}
function earth_greeting() {
echo 'hello earth';
}
class Echo_Two
{
public function __invoke($baz, $bat)
{
echo $baz, " ", $bat;
}
}
class Eat_Static
{
static function another()
{
echo 'Another example.';
}
}
class Foo
{
public function more()
{
echo 'And here is another one.';
}
}
wait_do_linebreak(0, 'earth_greeting');
$my_echo = function($str) {
echo $str;
};
wait_do_linebreak(0, $my_echo, 'hello');
wait_do_linebreak(0, function() {
echo "I'm on top of the world.";
});
wait_do_linebreak(0, new Echo_Two, 'The', 'Earth');
wait_do_linebreak(0, ['Eat_Static', 'another']);
wait_do_linebreak(0, [new Foo, 'more']);
$array = [
'jim',
'bones',
'spock'
];
$word_contains_o = function (string $str) {
return strpos($str, 'o') !== false;
};
print_r(array_filter($array, $word_contains_o));
输出:
hello earth
hello
I'm on top of the world.
The Earth
Another example.
And here is another one.
Array
(
[1] => bones
[2] => spock
)
可调用(回调)函数是在另一个函数内部调用或用作另一个函数的参数的函数
// An example callback function
function my_callback_function() {
echo 'hello world!';
}
// Type 1: Simple callback
call_user_func('my_callback_function');
在某些情况下,您的函数是其他函数的模板,在这种情况下,您可以使用可调用函数的参数。
了解更多信息: http://php.net/manual/en/language.types.callable.php
callable
是一种php数据类型。它只是意味着任何可以被调用的东西,即函数类型。如果这个函数是一个闭包,静态/常规方法或其他什么都没关系,只要我们可以调用该函数即可。
//php callable type
$callback = function() {
return "hello world!\n";
};
class MyClass {
static function myCallbackMethod() {
return "static method call \n";
}
public function cb()
{
return "method call \n";
}
public function __invoke() {
return "invoke \n";
}
}
$obj = new MyClass();
// Illustrative function
function soUseful (callable $callback) {
echo $callback();
}
soUseful($callback);
soUseful(array($obj, 'cb')); // syntax for making method callable
soUseful(array('MyClass', 'myCallbackMethod')); // syntax for making static method callable
soUseful($obj); // Object can be made callable via __invoke()
soUseful(fn() => "hi from closure \n"); // arrow fn
//Output
//hello world!
//method call
//static method call
//invoke
//hi from closure
Callable 是一种数据类型。
注意:您始终可以使用内置的 is_callable 函数检查变量是否属于“可调用”类型,并将变量的处理程序作为其参数。
代码中看到的“callable”关键字用于“类型声明”,在 PHP 5 中也称为“类型提示”。这用于指定您的函数或方法接受哪种类型的参数或形参。这是通过简单地将“类型提示”或“类型声明”(即类型的名称,如本例中的“可调用”)放在参数名称之前来完成的。
每当在函数声明中使用“类型提示”或“类型声明”时(即,当您指定允许/接受哪些类型时),并且您调用它们时给出的数据类型参数除了指定为可接受的数据类型之外,产生错误。
注意:此外,如果您想让您的函数需要>从特定类实例化的对象,则可以使用类名< for its respective parameter
-
参考资料:
-
我是编码新手,所以请纠正我的错误:)
Use callable to require a callback function as an argument
We use it with argument of function it will strict to to pass callable( closure static/regular function ) when we calling the function
示例 1 在函数调用中未传递 Callable :
$myfuntion = function(callable $sum){
};
// when calling myfuntion i passed integer
$myfuntion(123);
// it will not allow us to do this
示例 2 在函数调用中传递 Callable :
$sum = function($arr){
echo array_sum($arr);
};
$myfuntion = function(callable $sum){
// do something
};
$myfuntion($sum);
// Sum is a callable function now it will work absolutely fine
$sum = function( ...$args){
echo array_sum($args);
};
$myfun = function(callable $sum){
$sum(1,2,3,4,5);
};
$myfun($sum);