如何在php中使用可调用伪类型将一个函数用作另一个函数的参数?

如何在php中使用可调用伪类型将一个函数用作另一个函数的参数?,php,function,echo,callable,Php,Function,Echo,Callable,我刚刚从php.net上读到了关于callable类型的内容,并假设使用callable关键字应该允许将函数作为参数传递给另一个函数。但是,我得到了警告。以下是我尝试过的: <?php function helloWorld() { echo 'Hello World!'; } function handle(callable $fn) { $fn(); } handle(helloWorld); // Outputs: Hello World! ?> 有时 W

我刚刚从php.net上读到了关于callable类型的内容,并假设使用
callable
关键字应该允许将函数作为参数传递给另一个函数。但是,我得到了警告。以下是我尝试过的:

<?php

function helloWorld()
{
   echo 'Hello World!';
}
function handle(callable $fn)
{
   $fn(); 
}

handle(helloWorld); // Outputs: Hello World!

?>
有时

Warning: Use of undefined constant helloWorld - assumed 'helloWorld' (this will throw an Error in a future version of PHP) in C:\Projects\Sandbox\myphp on line 12
问题1。为什么php希望
helloWorld
是一个变量,而它已经被明确定义为一个函数。

问题2。显然,删除函数定义中的关键字
callable
,没有什么区别。为什么?

你应该用引号引这个论点,如下所示:

handle('helloWorld');
而不是

handle(helloWorld);
“PHP函数以其名称作为字符串传递”的状态

handle(helloWorld);