我刚刚从 php.net 阅读了有关可调用类型的信息,并假设使用callable关键字应该允许将函数作为参数传递给另一个函数。但是,我收到警告。这是我尝试过的:<?phpfunction 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 12Q1。为什么 php 期望helloWorld成为一个变量,它已经被明确定义为一个函数。Q2。显然,删除函数定义中的关键字callable没有任何区别。为什么?
1 回答
慕桂英546537
TA贡献1848条经验 获得超10个赞
您应该将参数放在引号中,如下所示:
handle('helloWorld');而不是
handle(helloWorld);
PHP Docscallable声明“ PHP 函数通过其名称作为字符串传递”。
- 1 回答
- 0 关注
- 162 浏览
添加回答
举报
0/150
提交
取消
