function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}
它对我们有什么好处?
我们为什么以及如何使用它?
也许这对你来说非常基础,但我已经尝试搜索了很久,却找不到答案。至少,我理解不了。
希望能得到傻瓜式的回答。我是编码新手... XD
编辑:这里是我从链接中复制上面一段代码的地方。
function post($pattern, callable $handler) {
$this->routes['post'][$pattern] = $handler;
return $this;
}
它对我们有什么好处?
我们为什么以及如何使用它?
也许这对你来说非常基础,但我已经尝试搜索了很久,却找不到答案。至少,我理解不了。
希望能得到傻瓜式的回答。我是编码新手... XD
编辑:这里是我从链接中复制上面一段代码的地方。
这是一种类型提示,告诉我们该函数将$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
的回调函数,它会以你想要的自定义方式记录数据。...$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(闭包静态/常规函数)
示例1:在函数调用中不传递可调用函数:
$myfuntion = function(callable $sum){
};
// when calling myfuntion i passed integer
$myfuntion(123);
// it will not allow us to do this
示例2:在函数调用中传递可调用对象:
$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);
Callable是一种数据类型。
注意:您可以始终使用内置的is_callable函数,通过将变量处理程序作为其参数来检查变量是否为“callable”类型。
在代码中看到的“callable”关键字用于“类型声明”,也称为PHP 5中的“类型提示”。这用于指定函数或方法接受哪种类型的参数或参数。只需在参数名称之前放置“类型提示”或“类型声明”(即类型名称,如此例中的“callable”)即可完成此操作。
每当在函数声明中使用“类型提示”或“类型声明”(即当您指定允许/接受哪些类型时),并且您调用它们并提供与指定的可接受数据类型不同的参数时,都会生成错误。
注意:还可以使用类名,如果您想使函数需要>从特定类实例化的对象<作为其相应参数
-
参考资料:
-
我是编程新手,请纠正我的错误 :)
callable
是参数的一种类型提示 - 可以查看http://php.net/manual/zh/language.types.callable.php了解什么是`callable`。 - Nigel Ren