我刚刚从 php.net 阅读了有关可调用类型的信息,并假设使用callable关键字应该允许将函数作为参数传递给另一个函数。但是,我收到警告。这是我尝试过的:
<?php
function helloWorld()
{
echo 'Hello World!';
}
function handle(callable $fn)
{
$fn();
}
handle(helloWorld); // Outputs: Hello World!
?>
但是,我有时会收到以下错误:
Parse error: syntax error, unexpected 'function' (T_FUNCTION), expecting variable (T_VARIABLE)
而有时
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
Q1。为什么 php 期望helloWorld成为一个变量,它已经被明确定义为一个函数。
Q2。显然,删除函数定义中的关键字callable没有任何区别。为什么?
慕桂英546537