php - 类型提示如何与 Closure::call 一起使用?
问题描述
我创建了一个名为的函数foo
,它调用\Closure
在bar
.
Closure::call()这里的文档说该call
方法“临时将闭包绑定到 newthis,并使用任何给定的参数调用它”。
在我的示例中,我没有任何参数,但确实baz
从我正在调用调用的对象调用方法。执行代码时,它会按预期回显“Hello world”。但是,我的 IDE 无法检测到baz
anonymus 函数中的方法,而是标记了一个警告:Method baz not found in A
.
class A
{
public function bar(): string
{
$b = new B();
return $b->foo(function(){
return $this->baz();
});
}
}
class B
{
public function foo(\Closure $callback): string
{
$callback->call($this);
}
public function baz(): string
{
return "Hello world!";
}
}
我可以添加这样的东西。
$b->foo(function(){
/** @var B $this */
return $this->baz();
});
但是,如果我运行像 php stan 这样的静态代码分析。然后它报告:
Call to an undefined method A::baz().
因此,我的问题是:我应该如何newthis
在我的匿名函数中引用,这甚至可能吗?
解决方案
推荐阅读
- ios - 来自 ARKit 的面部纹理
- excel - 如何在其他工作簿打开时自动更新 Vlookup 链接?
- php - 使 PHPBB 3.0.14 和 ABBC3 与 PHP 7.3 兼容
- reactjs - ReactJS:调整窗口大小时如何阻止屏幕刷新状态?
- python - 如何使用人员 API 创建新联系人
- babeljs - 错误:[BABEL] /home/vsts/work/1/s/src/index.js:找不到模块'./src/data'
- python - Python TypeError: isinstance() arg 2 必须是类型或类型的元组
- mql4 - 检查 Metatrader 4 交易历史中 OrdersHistoryTotal 的数量是否改变
- scala - 在 Spark 中是否可以在同一个 SparkSession 上运行并发作业?
- xamarin.forms - iOS13中如何处理用户与新蓝牙提示的交互?