我有一个类测试,它初始化一个变量并注册一些匿名函数。一个函数用于显示变量testvar,另一个匿名函数用于将变量替换为另一个变量。问题是,当我第二次调用显示函数时,结果是a variable,但应该是another variable。希望您理解这个例子,非常感谢。
class test {
private $functions = array();
private $testvar;
function __construct() {
$this->testvar = "a variable";
$this->functions['display'] = function($a) { return $this->display($a); };
$this->functions['replace'] = function($options) { return $this->replace($options); };
}
private function display($a) {
return $this->$a;
}
private function replace($options) {
foreach($options as $a => $b) {
$this->$a = $b;
}
}
public function call_hook($function, $options) {
return call_user_func($this->functions[$function], $options);
}
}
$test = new test();
echo $test->call_hook("display","testvar");
$test->call_hook("replace",array("testvar","another variable"));
echo $test->call_hook("display","testvar");