从array_map匿名函数内部调用类方法

Ben*_*rey 4 php scope class object array-map

我试图从array_map匿名函数中调用我的对象的一个​​方法.到目前为止,我收到了预期的错误:

致命错误:在不在对象上下文中时使用$ this ...

我知道为什么我会收到这个错误,我只是不知道如何实现我想要的...有人有任何建议吗?

这是我目前的代码:

// Loop through the data and ensure the numbers are formatted correctly
array_map(function($value){
    return $this->some_method($value,'value',false);
},$this->mssql->data[0]['results'][0]);
Run Code Online (Sandbox Code Playgroud)

Cil*_*ier 6

您可以使用"use"关键字告诉函数"关闭"$ this变量

$host = $this;
array_map(function($value) use ($host) {
    return $host->some_method($value,'value',false);
},$this->mssql->data[0]['results'][0]);
Run Code Online (Sandbox Code Playgroud)