从array_map匿名函数内部调用类方法 [英] Call class method from inside array_map anonymous function
本文介绍了从array_map匿名函数内部调用类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图从 array_map
匿名函数中调用对象的方法之一。到目前为止,我收到了预期的错误:
I am trying to call one of my object's methods from within an array_map
anonymous function. So far I am receiving the expected error of:
致命错误:在不在对象上下文中时使用$ this ...
Fatal error: Using $this when not in object context in...
我知道我为什么会收到此错误,我只是不知道一种方法来实现我想要的目标...有人吗?有什么建议吗?
I know why I am getting this error, I just don't know a way to achieve what I am trying to... Does anybody have any suggestions?
这是我当前的代码:
// 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]);
推荐答案
您可以告诉函数关闭通过使用 use关键字来使用$ this变量
You can tell the function to "close over" the $this variable by using the "use" keyword
$host = $this;
array_map(function($value) use ($host) {
return $host->some_method($value,'value',false);
},$this->mssql->data[0]['results'][0]);
这篇关于从array_map匿名函数内部调用类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文