有序阵列我有以下的PHP类,它包含很多其他的应用程序为中心的功能my_new_iterative_function()
旁边,但是当我进入foreach
的$this
(我需要)范围变因上下文无效。通过$this
以使其在method_foo
和method_bar
内有效的正确方法是什么?
注意:这是一个更为复杂的问题的一部分,并且所述$fallback_order
在缺省顺序执行功能,但是my_new_iterative_function()
需要接受的阵列,以控制的执行顺序(它是$order_functions
阵列的目的
class Foo {
public function my_new_iterative_function(array $fallback_order = array('method_foo', 'method_bar')) {
$order_functions = array(
'method_foo' => function(){
// need to access $this
},
'method_bar' => function(){
// need to access $this
},
);
foreach ($fallback_order as $index => $this_fallback) {
$order_functions[$this_fallback]();
}
}
}
$instance_of_foo->my_new_iterative_function();
$instance_of_foo->my_new_iterative_function([ 'method_bar', 'method_foo', ]);
不能测试这个,因为我在工作,而且我们没有支持这个的PHP版本,但尝试将函数定义为''method_foo'=> function()use($ this){' – Izkata