I'm having trouble passing a reference around a closure:
class stdobject {
public function __call($method, $arguments) {
if (isset($this->{$method}) && is_callable($this->{$method})) {
return call_user_func_array($this->{$method}, $arguments);
} else {
throw new Exception("Fatal error: Call to undefined method: $method");
}
}
}
$mod=function(){
$test=new stdobject();
$mode;
$test->init=function($params) use (&$mode) {
$mode =& $params['opmode'];
};
$test->setup=function() use (&$mode) {
$mode='test';
};
return $test;
};
$opmode='helloworld';
$test=$mod();
$test->init([ 'opmode' => &$opmode ]);
$test->setup();
echo $opmode; //should display test
I would like the setup function to modify $opmode in the outside scope, which would possibly not be the global scope, can anyone point me in the right direction on how to achieve this?